I'm trying to parse dates in the form of "Sept 9, 2021" using datetime
I was trying the following format:
"%m %d, %y" but it raises a ValueError while I'm trying to convert it
(i.e using int(datetime.strptime("Sept 9, 2021","%m %d, %y").timestamp()))
How can I tell which format am I supposed to use?
Thanks
For strptime there are different format code than usual.
You use %b to refer to a Three-digit string month, which means that you must use Sep instead of Sept, and you use %Y to convert a 4 digit year.
So the code would be like the following
int(datetime.datetime.strptime("Sep 9, 2021", "%b %d, %Y").timestamp())
# 1631138400
For more information see Format Codes.
To my knowledge the key work for September is Sep (not Sept).
You can check this link for more details on that: Python Date abbreviation
I advice you to use something similar to this snippet of code:
from datetime import datetime
date_s = "Sep 9, 2021"
datetime_object = datetime.strptime(date_s.upper().replace("SEPT", "SEP"), '%b %d, %Y')
print(datetime_object)
You can also check this Stack thread that seem similar to your question:
Date Handling in Python for Sept
There are a couple of ways how to get the timestamp from your string:
First solution is to use dateparser module from PyPi (pip install dateparser). This module can help you to get datetime object from any string which is similar to datetime string.
Solution:
import dateparser
date_time_object = dateparser.parse("Sept 9, 2021").timestamp()
print(date_time_object)
Result: 1631127600.0
Another issue is that your string contains one unnecessary string in name of month so this solution is a bit wrong and won't help you but only for understanding, I will offer this:
Solution:
from datetime import datetime
time_stamp = int(datetime.strptime("Sep 9, 2021","%b %d, %Y").timestamp())
print(time_stamp)
Result: 1631127600
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 am relatively new to python.
I have a timestamp of the format - 2016-12-04T21:16:31.265Z. It is of a type string. I want to know how can I parse the above timestamp in python.
I was looking through the datetime library, but seems like it accepts only floats. How do I get the time stamp parsed? I was trying to hunt for something like an equivalent of Instant (in java) for python?
import datetime
time_str = '2016-12-04T21:16:31.265Z'
time_stamp = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S.%fZ")
print(time_stamp)
Reference: https://docs.python.org/2/library/datetime.html; (8.1.7. strftime() and strptime() Behavior)
To parse it according to your current timezone, using the format used by the Unix date command:
import re
from calendar import timegm
from datetime import datetime
from time import localtime, strptime, strftime
fmt = "%a %b %d %H:%M:%S %Z %Y"
ts = "2016-12-04T21:16:31.265Z"
strftime(fmt, localtime(timegm(strptime(re.sub("\.\d+Z$", "GMT", ts), '%Y-%m-%dT%H:%M:%S%Z'))))
I'm using the following code:
data['Input_volTargetStart'][1]>time.strptime(data['Dates'][1], "%d %b $y")
When I try to run it, I get this error:
ValueError: time data '04-Jun-99' does not match format '%d %b $y'
I have tried possibly all combinations but am unable to get the result for this conversion.
You can actually put the dashes ('-') as part of the format, i.e "%d-%b-%y".
This is what you'll need.
from datetime import datetime
a = "04-Jun-99"
frmt = datetime.strptime(a, "%d-%b-%y")
print(frmt)
>>1999-06-04 00:00:00
Implement that example into your code to get proper output.
I have string date data with the following format:
4/16/15 23:50
When I try to convert into a datetime object:
print datetime.datetime.strptime(fecha2, '%d/%m/%y %H:%M')
I get this error:
ValueError: time data '4/16/15 23:50' does not match format '%d/%m/%y %H:%M'
According to this list:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior, I am using the right format. Where am I wrong?
you have month and day reversed:
print datetime.datetime.strptime(fecha2, '%m/%d/%y %H:%M')
Also very handy:
from dateutil.parser import parse
parse("4/16/15 23:50")
I know this topic is old but maybe it will help someone. I have faced recently similar issue where the date was actually correctly typed and it throwed the same error:
datetime.datetime.strptime('2018-05-02T14:27:56+00:00', "%Y-%m-%dT%H:%M:%S%z")
error: time data '2018-05-02T14:27:56+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
The issue was that i was trying to run the python 3.7 code with python 3.6
I'm using the Paypal API and I get back a timestamp in the following format. I try to parse this to a datetime object using strptime, but I get the following error:
(Pdb) datetime.strptime('2012-03-01T10:00:00Z','%Y-%M-%dT%H:%M:%SZ')
*** error: redefinition of group name 'M' as group 5; was group 2
Also, as this format is supposed to be quite a standard format isn't there a function available for this?
EDIT:
Ok seems to be a typo. First %M should be %m
The parser from dateutil is your friend.
You'll have to pip install dateutil but you've save bags and bags of date conversion code:
pip install python-dateutil
You can use it like this.
from dateutil import parser
ds = '2012-03-01T10:00:00Z' # or any date sting of differing formats.
date = parser.parse(ds)
You'll find you can deal with almost any date string formats with this parser and you'll get a nice standard python date back
Looks like you're mixing %M (minute) and %m (month).
The problem is that you use %M twice. Use %m for the months:
>>> datetime.strptime('2012-03-01T10:00:00Z','%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2012, 3, 1, 10, 0)
You have a typo. %M is used twice. You meant to use %m for the month. From the docs:
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].