My date is in following format:
19/Jun/2014:00:03:09
How to I convert it to epoch timestamp in python?
Note: I searched on date format in python, but could not find any format that matches above.
Thanks.
Use strptime and then mktime.
import time
tt = time.strptime("19/Jun/2014:00:03:09","%d/%b/%Y:%H:%M:%S")
print time.mktime(tt)
import datetime
datetime.datetime.strptime(s, '%d/%b/%Y:%H:%M:%S')
reference
use dateutil it will parse just about anything
$ easy_install python-dateutil
>>> import dateutil.parser as parser
>>> some_date_string = "19/Jun/2014:00:03:09"
>>> parser.parse(some_date_string)
[edit] oops nevermind ... apparently it cant parse this ...
Related
I am currently having an issue converting an incoming datetime string to a datetime object using Python's built in strptime() method. Here is what I currently have:
def fixDateTimeField(datetimeString):
# Convert from 2012-08-07T00:00:00Z to 2012-08-07T00:00:00.000Z
datetime_object = datetime.strptime(datetimeString, "%y-%m-%dT%H:%M:%SZ")
return datetime_object
Like the comment says, I am trying to convert the string "2012-08-07T00:00:00Z" to a datetime object that looks like 2012-08-07T00:00:00.000Z but I am getting an error in my console that says: "ValueError: time data '2012-08-07T00:00:00Z' does not match format '%y-%m-%dT%H:%M:%SZ'". The format seems correct to me and i'm not seeing the issue.
Thanks in advance!
%y is for two-digit years. You have a four-digit year.
Try using %Y instead.
>>> from datetime import datetime
>>> datetimeString = "2012-08-07T00:00:00Z"
>>> print(datetime.strptime(datetimeString, "%Y-%m-%dT%H:%M:%SZ"))
2012-08-07 00:00:00
A nice way to parse your iso-8601 datetime string "2012-08-07T00:00:00Z" to a datetime object is using dateutil.
import dateutil.parser
yourdate = dateutil.parser.parse(datestring)
With strptime:
datetime.datetime.strptime( "2007-03-04T21:08:12", "%Y-%m-%dT%H:%M:%S")
Works. As an other answer said, the Y must be capitalized for strptime to work with 4 digit years.
I want to display the datetime in the following format using python:
2018-06-25T07:17:17.000Z
I am trying to convert using strftime:
datetime.datetime.now().strftime("%Y-%m-%dTH:M:SZ")
but it seems that doesn't work.
What i am missing?
you can use
import datetime
datetime.datetime.today().isoformat()
2018-06-25T07:17:17.000Z
This format is called ISO format, after standard ISO 8601. The datetime object has a isoformat method to output this form.
strftime("%Y-%m-%dTH:M:SZ")
You seem to have forgotten some % before the H, M, and S. Try strftime("%Y-%m-%dT%H:%M:%SZ").
but it seems that doesn't work.
Generally it works better if you specify exactly what doesn't work, or what you expect and how the reality differs from your expectation.
You can use following formating for date conversion.
>>> import datetime
>>> today_date = datetime.datetime.now()
>>> today_date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
'2018-06-25T15:50:18.313620Z'
Please let me know,if this is the one you needed.
I have a string date as 2015-03-25T00:00:00Z. How do I convert it to a unix epoch1426636800000.0
Are there any libraries in python to do that.
Using time, for example.
So first you need to convert the string to time object (or you can use datetime alternatively as halex mentioned) and then get the seconds since epoch.
>>> import time
>>> time.mktime(time.strptime('2015-03-25T00:00:00Z', '%Y-%m-%dT%H:%M:%SZ'))
1427241600.0
time.strptime(string[, format])
import time
print time.strptime("2015-03-25T00:00:00Z","%Y-%m-%dT%H:%M:%SZ")
If you have Python 3.3 or newer you can use the datetime module:
>>> import datetime
>>> datetime.datetime.strptime("2015-03-25T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ").timestamp()
1427238000.0
You can use easy_date to make it easy:
import date_converter
timestamp = date_converter.string_to_timestamp("2015-03-25T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ")
I have been reading quite some time answers and couldn't really drive into results.
I have the following code:
>>>from datetime import datetime
>>>a = '2013-08-23T23:37:38+0000'
>>>dt = datetime.strptime(a,'%Y-%m-%dT%H:%M:%S+0000')
>>>print dt.date()
2013-08-23
>>>print dt.time()
23:37:38
What is the simplest way to output this result (assuming of course that a is unknown) given that we live in Central Europe. So it should be "daylight saving-proof" as well.
In winter:
2013-08-24
00:37:38
In summer:
2013-08-24
01:37:38
If it only needs default libraries it would be great.
Some more info
I dived into the libraries after my question. My above 3rd line should be better be:
dt = datetime.strptime(a,'%Y-%m-%dT%H:%M:%S%z'). However a bug came out in 2.7.5 python (OS X if it matters) and had some trouble finding the %z. If you have trouble change version, if not ignore this. Of course the strptime() is just simpler level of dateutil.parser mentioned in the two answers so it can better be used instead of my code above.
you can use the useful dateutil package and the pytz one for exemple convert to paris timezone
from dateutil import parser
import pytz
FR = pytz.timezone('Europe/Paris') # there is the summer offset changing in this zone
date = parser.parse("2013-08-23T23:37:38+0000")
datefr = date.astimezone(FR)
Here's an example using pytz and dateutil:
from dateutil import parser
import pytz
date = parser.parse('2013-08-23T23:37:38+0000')
CET = pytz.timezone('CET')
date = date.astimezone(CET)
print date.date() # prints 2013-08-24
print date.time() # prints 01:37:38
Is there a python module specifically for date manipulation. Something equivalent to the lubridate package in R.
Python have a built-in module for handling datetime..You can try that..!!
But if want something extended (like want to build a generic datetime parser), go for python-dateutil
from dateutil.parser import parse
datetimeObj = parse(strDate)
# str date is a date string
Actually, Python's standard datetime module is pretty basic. If you want more extensive and flexible date handling, you can try dateutil or mxDateTime.
The python module arrow seems to be an equivalent of the R lubridate package:
import arrow
In [9]: arrow.get('Julie was born in May 1990', 'MMM YYYY')
Out[9]: <Arrow [1990-05-01T00:00:00+00:00]>
Try to use pytz
Documentation
http://pytz.sourceforge.net/