0

I am currently trying to convert a date in the following format YYYYmmddHHMMSS to a unix timestamp but I get an error (ValueError: year is out of range).

import datetime

def ts(date):
    return datetime.datetime.fromtimestamp(date).strftime('%Y%m%d%H%M%S')

if __name__ == "__main__":
    date = 20130814100000
    print ts(date)
3
  • 5
    Why did you delete your SSCCE? Also, can you please post the full stack trace? Commented Aug 14, 2013 at 12:15
  • 1
    There was a big mistake in the SSCCE, it was a TS to DATE example, while I am asking for a DATE to TS function... My mistake. I am flagging the post to suppress it. Sorry. Commented Aug 14, 2013 at 12:18
  • 2
    You should be able to delete it yourself and rewrite it. Commented Aug 14, 2013 at 12:18

4 Answers 4

5

Your date should be a string. Here is how you do it. (If your date is an integer then just do date = str(date).

>>> import time
>>> from datetime import datetime
>>> date = '20130814100000'
>>> dt = datetime.strptime(date, '%Y%m%d%H%M%S')
>>> print dt
2013-08-14 10:00:00
>>> print time.mktime(dt.timetuple())
1376467200.0

time also has a strptime function but it returns a not so useful struct_time object. But if you only need a unix time, then you can use it too:

>>> time.mktime(time.strptime(date, '%Y%m%d%H%M%S'))
1376467200.0
Sign up to request clarification or add additional context in comments.

Comments

4

I think the problem here is that .fromtimestamp() is expecting a Unix timestamp, not a date formatted as YYYYmmdd...

To parse the date information that you do have there, I'd recommend using .strptime() or the excellent python-dateutil package.

import datetime

def ts(date):
    stamp = datetime.datetime.strptime(date, '%Y%m%d%H%M%S')
    return stamp.strftime('%Y%m%d%H%M%S')

or

from dateutil.parser import parse

def ts(date):
    stamp = parse(date)
    return stamp.strftime('%Y%m%d%H%M%S')

http://labix.org/python-dateutil

Comments

2
  • The function that parses datetime is called strptime, not strftime (which formats time).
  • 20130814100000 is not an UNIX timestamp
  • strptime takes string as argument

Overall, your code should look like:

import datetime

def ts(date):
    return datetime.datetime.strptime(date, '%Y%m%d%H%M%S')

if __name__ == "__main__":
    date = "20130814100000"
    print ts(date)

Comments

0

You seem to have some confusion over the different ways that times are represented. The value you have assigned to date appears to already be a formatted timestring of "2013-08-14 10:00:00", but you're passing it into fromtimestamp. This function expects a Unix timestamp, which is simply the number of seconds that have elapsed since Midnight on Jan 1st 1970.

I believe something like this is what you're looking for:

import datetime

def ts(datestr):
    return datetime.datetime.strptime(datestr, "%Y%m%d%H%M%S")

if __name__ == "__main__":
    date = 20130814100000
    print ts(date)

strftime like you had is for formatting times into strings. strptime is for parsing strings into times.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.