I have a Python date like
2013-04-04T18:56:21Z
I want to store this into my mysql database .
I tried like
self.start_at = datetime.strptime(self.start_at.split(".")[0], "%Y-%m-%dT%H:%M:%S")
But i am getting an error
ValueError: unconverted data remains: Z
PLease tell me how to convert the above date in mysql acceptable format .
In this instance this will work.
>>> from datetime import datetime
>>> start_at = '2013-04-04T18:56:21Z'
>>> datetime.strptime(start_at , "%Y-%m-%dT%H:%M:%SZ")
datetime.datetime(2013, 4, 4, 18, 56, 21)
Are the T & Z characters always in your date format or do they every change?
If the is some variation in these seperator characters, you should do something like this:
>>> from datetime import datetime
>>> start_at = '2013-04-04T18:56:21Z'
>>> datetime.strptime(start_at[0:10] + ' ' + start_at[11:19], "%Y-%m-%d %H:%M:%S")
datetime.datetime(2013, 4, 4, 18, 56, 21)
Related
I have this timestamp string:
x = 2021-11-24T16:05:51.399+0000
I am struggling to get the parsing to pass after the decimal point. Here is my attempt so far:
datetime.datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%f+")
Current error running this line:
ValueError: unconverted data remains: 0000
Add %z at the end of the format to match UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
>>> from datetime import datetime
>>>
>>> x = '2021-11-24T16:05:51.399+0000'
>>> datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%f%z")
datetime.datetime(2021, 11, 24, 16, 5, 51, 399000, tzinfo=datetime.timezone.utc)
I have data like this: "2016-10-17 09:34:02" with the format: "%Y-%m-%d %H:%M:%S" and I use: from datetime import datetime
My variable is like this:
date_object = datetime.strptime(datetemp, format)
So far, so good...
But I need get only the time part from this object (date_object) to make some comparisons...
When I do this:
print(date_object.time)
I get the following error:
built-in method time of datetime.datetime object at 0x00A94AE8
How can i get just the time part from the object date_object? This will allow me to make comparisons within hourly ranges.
You need to add parenthesis () to your print(date_object.time) call:
print(date_object.time())
Example output:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.time()
datetime.time(22, 14, 6, 996000)
>>> print(now.time())
22:14:06.996000
>>> import datetime as dt
>>> timenow = dt.datetime.now()
>>> timenow
datetime.datetime(2016, 11, 23, 9, 59, 54, 291083)
>>> timenow.date()
datetime.date(2016, 11, 23)
>>> timenow.time()
datetime.time(9, 59, 54, 291083)
the outputs are in datetime type.
>>> print(datetime.time())
00:00:00
or you can convert it into a string and print
>>> time_str = str(timenow.time())
>>> time_str
'09:59:54.291083'
date_object.time().__str__() this will give you the format you want
date_object.time().__str__() is equal to `print(date_object.time())`
but you can store it in a var, not just print it in you screen.
I am getting below error while trying to do this
from datetime import datetime
time1 = '2016-08-01 13:39:00+05:30'
x = datetime.strptime(time1, '%Y-%m-%d %H:%M:%S %z')
print(x)
Error is ...
ValueError: time data '2016-08-01 13:39:00+05:30' does not match format '%Y-%m-%d %H:%M:%S %z'
If you are using Python 2 or early versions of Python 3 (3.0 and 3.1), you can use the dateutil library for converting a string to a timezone aware object.
The code to do this is simple:
>>> import dateutil.parser
>>> dt = dateutil.parser.parse('2016-08-01 13:39:00+05:30')
>>> dt
datetime.datetime(2016, 8, 1, 13, 39, tzinfo=tzoffset(None, 19800))
If you are using Python 3.2 or later, the %z option has been added as a formatting option when parsing a date. You can accomplish this task without using dateutil in these versions by doing this:
>>> import datetime
>>> dt = datetime.datetime.strptime('2016-08-01 13:39:00+0530', "%Y-%m-%d %H:%M:%S%z")
>>> dt
datetime.datetime(2016, 8, 1, 13, 39, tzinfo=datetime.timezone(datetime.timedelta(0, 19800)))
Unfortunately, you do have to strip the colon (:) from the offset for this to work as expected.
this works in python 3.4, conforms to the datetime documentation
from datetime import datetime
time1 = '2016-08-01 13:39:00+0530'
x = datetime.strptime(time1, '%Y-%m-%d %H:%M:%S%z')
print(x)
gives
2016-08-01 13:39:00+05:30
Consider using dateparser:
>>> dateparser.parse('2016-08-01 13:39:00+05:30')
datetime.datetime(2016, 8, 1, 13, 39)
>>> dateparser.parse('2016-08-01 13:39:00+05:30', settings={'TO_TIMEZONE': 'UTC'})
datetime.datetime(2016, 8, 1, 8, 9)
I have url that returns date in this format
url_date = "2015-01-12T08:43:02Z"
I don't know why there are strings, it would have been simpler to get it as "2015-01-1208:43:02" which would have been simpler to parse using
datetime.datetime.strptime(url_date , '%Y-%m-%d')
but it does not work. I have tried with
%Y-%m-%d
%Y-%m-%d-%H-%M-%S
%Y-%m-%d-%H-%M-%S-%Z
But I keep getting errors like "time data 2015-01-12T08:43:02Z does not match ..."
The format you are looking for is - '%Y-%m-%dT%H:%M:%SZ' .
Example -
>>> url_date = "2015-01-12T08:43:02Z"
>>> import datetime
>>> datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2015, 1, 12, 8, 43, 2)
For the new requirement in comments -
if I wanted to get a time back with the strings as in 2015-01-12:08:43:02 which methods should after datetime().datetime()
You would need to use .strftime() on the datetime.datetime object with the format - '%Y-%m-%d:%H:%M:%S'. Example -
>>> url_date = "2015-01-12T08:43:02Z"
>>> dt = datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
>>> dt.strftime('%Y-%m-%d:%H:%M:%S')
'2015-01-12:08:43:02'
If you wanted the time component , you can use .time() for that. Example -
>>> dt = datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
>>> dt.time()
datetime.time(8, 43, 2)
You were getting close with the "Z" in your final attempt - you need to specify the T, Z, and colon literal values in your format string.
>>> import datetime
>>> url_date = "2015-01-12T08:43:02Z"
>>> datetime.datetime.strptime(url_date , '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2015, 1, 12, 8, 43, 2)
I use connect the sqlite3 of chrome 'History' file for analysis. But there are data for last_visit_time, which is timestamp, but I don't know how to change it to real time.
I test like this:
2012-04-18 23:22:11.084300 (utctime)
2012-04-19 01:22:11.084300 (myPC time)
12,979,264,931,952,304 (the time stamp in the data)
I printed the utctime and clicked a website, almost at the same time. So I get the statistic like above. 12,979,264,931,952,304 is the long int, so normal way to convert is not possible.
How can I convert the timestamp to a date?
The timestamp it stores is the number of microseconds since midnight UTC on January 1st, 1601. To convert this to the current time, you can add the number of microseconds you have to the epoch date, with help from this answer, like this:
>>> import datetime
>>> epoch_start = datetime.datetime(1601, 1, 1)
>>> delta = datetime.timedelta(microseconds=12979264931952304)
>>> epoch_start + delta
datetime.datetime(2012, 4, 18, 23, 22, 11, 952304)
To convert to your local timezone, you can use this method (note that I am currently UTC - 4, while it looks like you are UTC + 2):
>>> from dateutil import tz
>>> from_zone = tz.tzutc()
>>> to_zone = tz.tzlocal()
>>> utc_time = (epoch_start + delta).replace(tzinfo=from_zone)
>>> utc_time.astimezone(to_zone)
datetime.datetime(2012, 4, 18, 19, 22, 11, 952304, tzinfo=tzlocal())