Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to copy date from a csv file to my txt file, but the date is not converted correctly and I am not sure why.
For example, I have 2015/9/5 in my csv file, and whenever I convert it, it is written in my txt file as 1970-01-01, instead of 2015. Please see my code below. Thank you.

What I have tried:

C#
import datetime
[...my code].
# Return a value from csv file.
date = worksheet.cell(0, 0).value
# Convert the value to the date.
dat1 = datetime.date.fromtimestamp(date)

...and this gives me 1970-01-01 instead of 2015-09-05 for some reason.
Posted
Updated 8-Dec-16 19:05pm

1 solution

Check out this example:
from datetime import datetime

datetime_object = datetime.strptime('2015/9/5', '%Y/%m/%d').date()

datetime_string = datetime_object.strftime("%Y/%-m/%-d")

print 'As datetime object: ' + str(datetime_object)

print 'As string: ' + datetime_string

Refer to python 2.7 documentation for more details on:
1. strptime[^]
2. strftime[^]
3. strftime() and strptime() Behavior[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900