I am trying to encode datetime format for strings that are of the form: '06JAN2018' or '31DEC2017'.
I think this is format = '%d[xxxx]%Y' but I don't know how to encode the month portion of it.
Is there a list anywhere of every type of encoding possible for this?
The list of these codes is in the datetime module's strftime() and strptime() Behavior documentation.
%b: Month as locale’s abbreviated name.
In your case, 06JAN2018 is %d%b%Y.
If what you're actually looking to do is to encode a datetime object or Pandas/NumPy datetime array to strings, you'll probably need to do the uppercasing yourself:
>>> dt = datetime.datetime(2017, 12, 31)
>>> dt.strftime('%d%b%Y').upper() # or .str.upper() in Pandas
'31DEC2017'
Related
I tried this:
timestamp = "2021-01-22T11:36:52.387000+01:00"
timestampObject = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
But gave me error:
ValueError: unconverted data remains: .150000+01:00
What is the rest reprisenting and how do I convert the rest? Also what does the 'T' mean?
Because you also have to supply a format specifier to take care of the trailing microseconds and timezone specifier, like the error is telling you, see Conversion of datetime string with microseconds and ...milliseconds. Probably you need '.fZ'. See the datetime doc.
Also, the 'T' just stands for 'Time'; it separates the date-field from the time-field, for ease in parsing (with sed/perl/grep/regex/etc.). Makes it easy if you wanted to a) locate datetimes within a log or b) throw away/separate the time part from the date part.
The string format you have is actually a datetime in ISO format. Luckily datetime has a function for handling that, you don't have to worry about supplying a format specifier for the trailing time objects...
Do you want only the date?
>>> datetime.datetime.fromisoformat("2021-01-22T11:36:52.387000+01:00").date()
datetime.date(2021, 1, 22)
Or do you want datetime?
>>> datetime.datetime.fromisoformat("2021-01-22T11:36:52.387000+01:00")
datetime.datetime(2021, 1, 22, 11, 36, 52, 387000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600)))
This worked for me:
timestampObject = datetime.fromisoformat(
"2021-01-22T11:36:52.387000+01:00" ).date()
print('timestampObject.year: ', timestampObject.year)
timestampObject.year: 2021
I have a date in the format '%Y-%M-%d' for example '2017-08-01', that I'd like to convert to the format '%m-%d-%y' for example '8-1-2017'.
Only relevant examples I've found have been in php unfortunately.
from datetime import datetime
datetime.strptime("2017-08-01", '%Y-%m-%d').strftime('%m-%d-%y')
datetime.strptime("2017-08-01", '%Y-%m-%d')
#output
datetime.datetime(2017, 8, 1, 0, 0)
#output final
'08-01-17'
In the first part strptime , you are defining how the date is to you. In other words, you are turning your string into a datetime type instance. Then in the second part strftime you are formatting it the way you wish it to be.
Official definitions
date, datetime, and time objects all support a strftime(format) method,
to create a string representing the time under the control of an explicit format string.
Conversely, the datetime.strptime() class method creates
a datetime object from a string representing a date and
time and a corresponding format string.
I want to convert
2010-03-01 to 733832
I just found this toordinal code
d=datetime.date(year=2010, month=3, day=1)
d.toordinal()
from this
But i want something more like
d=datetime.date('2010-03-01')
d.toordinal()
Thanks in advance
You'll need to use strptime on the date string, specifying the format, then you can call the toordinal method of the date object:
>>> from datetime import datetime as dt
>>> d = dt.strptime('2010-03-01', '%Y-%m-%d').date()
>>> d
datetime.date(2010, 3, 1)
>>> d.toordinal()
733832
The call to the date method in this case is redundant, and is only kept for making the object consistent as a date object instead of a datetime object.
If you're looking to handle more date string formats, Python's strftime directives is one good reference you want to check out.
like this:
datetime.strptime("2016-01-01", "%Y-%m-%d").toordinal()
You need to firstly convert the time string to datetime object using strptime(). Then call .toordinal() on the datetime object
>>> from datetime import datetime
>>> date = datetime.strptime('2010-03-01', '%Y-%M-%d')
>>> date.toordinal()
733773
It is even better to create a function to achieve this as:
def convert_date_to_ordinal(date):
return datetime.strptime(date, '%Y-%M-%d').toordinal()
convert_date_to_ordinal('2010-03-01')
#returns: 733773
I have a timestamp like:
2014-01-01T05:00:00.000Z
How do I convert this so that I can easily get the month like "January"? And in general convert it to a nice format like:
January 1st, 2014
You can use datetime module. datetime.datetime expects a time string and its formatting and returns a datetime.datetime object, on which you can call strftime() to format it according to your needs.
>>> import datetime
>>> my_date = datetime.datetime.strptime("2014-01-01T05:00:00.000Z", "%Y-%m-%dT%H:%M:%S.%fZ")
>>> my_date.strftime('%d.%m.%Y')
01.01.2014
>>> date.strftime('%H:%M:%S %d.%m.%Y')
'05:00:00 01.01.2014'
There is also a python-dateutils module, which can do the same.
The strftime() method in datetime modulecan achieve this. It expects a string pattern explaining how you want to format your date.
import datetime
today = datetime.date.today()
print today.strftime('It is %d %b %Y')
The above code prints something like "It is 12 Nov 2015"
You can find more format codes at https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
I have a string that contains the date in this format:
full_date = "May.02.1982"
I want to use datetime.strptime() to display the date in all digits like: "1982-05-02"
Here's what I tried:
full_date1 = datetime.strptime(full_date, "%Y-%m-%d")
When I try to print this, I get garbage values like built-in-67732
Where am I going wrong? Does the strptime() method not accept string values?
Your format string is wrong, it should be this:
In [65]:
full_date = "May.02.1982"
import datetime as dt
dt.datetime.strptime(full_date, '%b.%d.%Y')
Out[65]:
datetime.datetime(1982, 5, 2, 0, 0)
You then need to call strftime on a datetime object to get the string format you desire:
In [67]:
dt.datetime.strptime(full_date, '%b.%d.%Y').strftime('%Y-%m-%d')
Out[67]:
'1982-05-02'
strptime is for creating a datetime format from a string, not to reformat a string to another datetime string.
So you need to create a datetime object using strptime, then call strftime to create a string from the datetime object.
The datetime format strings can be found in the docs as well as an explanation of strptime and strftime