DateTime
As the name suggests the DateTime module contains all the functionality necessary to work with datetime values such as convert datetime to a specific format, generate datetime objects, ensure criteria, work with timezones, and the like.
Below we have the import statement for getting access to all the methods.
Generate DateTime Objects¶
Timestamp¶
The timestamp method is used in order to retrieve the number of seconds passed since January of 1970, the Unix Epoch.
We are also able to invert the operations, meaning retrieve a DateTime object from a given timestamp using the fromtimestamp method.
Now¶
The now method is used with the purpose of retrieving the current DateTime.
We are also able to specify a specific time zone by providing the tzinfo argument:
DateTime.now(tzinfo='America/Los_Angeles')
DateTime(2024, 9, 23, 12, 18, 54, 904738, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
Convert DateTime Values¶
The DateTime module contains useful methods in order to work with date conversion, whether the goal is to convert any object to a DateTime or convert a DateTime object to another object. Let's see the implementation in practice:
String to DateTime¶
The most common scenario when working with DateTime objects is converting a date string to an object which represents any date format, for this we have the string_date_to_date_time method.
DateTime.string_date_to_date_time('20240923')
DateTime(2024, 9, 23, 12, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
The method can also accept a force_time argument, designed to format the time to a specific day period, the options are MIDDAY, NOW, FIRST_MINUTE, and LAST_MINUTE.
DateTime.string_date_to_date_time('20240923', force_time='LAST_MINUTE')
DateTime(2024, 9, 23, 23, 59, 59, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
DateTime.string_date_to_date_time('20240923', force_time='FIRST_MINUTE')
DateTime(2024, 9, 23, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
DateTime.string_date_to_date_time('20240923', force_time='NOW')
DateTime(2024, 9, 23, 18, 21, 38, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
As seen in our first example, if we omit the force_time argument, the time defaults to MIDDAY.
DateTime to String¶
There is also the other way around, converting a DateTime object to a string using the strftime method.
The method accepts an optional input called format which will dictate the representation of the string.
You can click here to learn more about the valid formats.
You can also convert a DateTime object to a more informal representation with the strftime_pretty method:
You can further enhance the functionality by introducing two optional flags: just_date and just_time. When just_date is set to True, only the date will be displayed. On the other hand, if just_time is set to True, only the time will be shown.
DateTime(2024, 9, 23, 12, 15, 40).strftime_pretty(just_date=True)
'Sep. 23, 2024'
DateTime(2024, 9, 23, 12, 15, 40).strftime_pretty(just_time=True)
'12:15 p.m.'
ISO Strings to DateTime¶
For any ISO formatted string, we can use the fromisoformat method in order to convert it to a DateTime object
DateTime.fromisoformat('20240923')
DateTime(2024, 9, 23, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
DateTime.fromisoformat('2024-09-23')
DateTime(2024, 9, 23, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
DateTime.fromisoformat('2024-09-23T12:15:40')
DateTime(2024, 9, 23, 12, 15, 40, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
Date to DateTime¶
Still on the conversion matter, but this time talking specifically about the conversion from Date objects to DateTime objects. To achieve this we can make use of the date_to_date_time method.
date_obj = Date(2024, 9, 23)
DateTime.date_to_date_time(date_obj)
DateTime(2024, 9, 23, 12, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))
Is today, today?¶
For verifying if a given date is today we can use the is_today method.