Date_&_Time.ipynb - Colaboratory
Date_&_Time.ipynb - Colaboratory
ipynb - Colaboratory
1. date – An idealized naive date, assuming the current Gregorian calendar always was, and
always will be, in effect. Its attributes are year, month, and day.
2. time – An idealized time, independent of any particular day, assuming that every day has
exactly 246060 seconds. Its attributes are hour, minute, second, microsecond, and tzinfo.
3. datetime – Its a combination of date and time along with the attributes year, month, day, hour,
minute, second, microsecond, and tzinfo.
4. timedelta – A duration expressing the difference between two date, time, or datetime
instances to microsecond resolution.
5. tzinfo – It provides time zone information objects.
6. timezone – A class that implements the tzinfo abstract base class as a fixed offset from the
UTC (New in version 3.2).
Date class
The date class is used to instantiate date objects in Python. When an object of this class is
instantiated, it represents a date in the format YYYY-MM-DD. The constructor of this class needs
three mandatory arguments year, month, and date.
https://colab.research.google.com/drive/1usLrMDEMugNDAExYrSvBC8KBOUHkmUUO#printMode=true 1/7
18/11/2023, 19:10 Date_&_Time.ipynb - Colaboratory
To return the current local date today() function of the date class is used. today() function comes
with several attributes (year, month, and day). These can be printed individually.
https://colab.research.google.com/drive/1usLrMDEMugNDAExYrSvBC8KBOUHkmUUO#printMode=true 2/7
18/11/2023, 19:10 Date_&_Time.ipynb - Colaboratory
Time class
The time class creates the time object which represents local time, independent of any day.
Constructor Syntax:
To create a date, we can use the datetime() class (constructor) of the datetime module.
The datetime() class requires three parameters to create a date: year, month, day.
The datetime() class also takes parameters for time and timezone (hour, minute, second,
microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).
import datetime
x = datetime.datetime(2024, 5, 23)
print(x)
2024-05-23 00:00:00
strftime() method
The datetime object has a method for formatting date objects into readable strings.
The method is called strftime(), and takes one parameter, format, to specify the format of the
returned string:
https://colab.research.google.com/drive/1usLrMDEMugNDAExYrSvBC8KBOUHkmUUO#printMode=true 4/7
18/11/2023, 19:10 Date_&_Time.ipynb - Colaboratory
https://colab.research.google.com/drive/1usLrMDEMugNDAExYrSvBC8KBOUHkmUUO#printMode=true 5/7
18/11/2023, 19:10 Date_&_Time.ipynb - Colaboratory
# Example 1
s = now.strftime("%A %m %-Y")
print('\nExample 1:', s)
# Example 2
s = now.strftime("%a %-m %y")
print('\nExample 2:', s)
# Example 3
s = now.strftime("%-I %p %S")
print('\nExample 3:', s)
# Example 4
s = now.strftime("%H:%M:%S")
print('\nExample 4:', s)
Example 2: Sat 11 23
Example 3: 11 AM 29
Example 4: 11:01:29
Python timedelta class is used for calculating differences in dates and also can be used for date
manipulations in Python. It is one of the easiest ways to perform date manipulations.
https://colab.research.google.com/drive/1usLrMDEMugNDAExYrSvBC8KBOUHkmUUO#printMode=true 6/7
18/11/2023, 19:10 Date_&_Time.ipynb - Colaboratory
# printing initial_date
print("initial_date", str(ini_time_for_now))
current system time. Tzinfo is an abstract base class in Python. It cannot be directly instantiated. A
# printing calculated future_dates
concrete subclass must derive fromstr(future_date_after_2yrs))
print('future_date_after_2yrs:', this abstract class and implement the methods offered by it.
print('future_date_after_2days:', str(future_date_after_2days))
Python DateTime
initial_date 2023-11-18timezone
11:01:01.952827
future_date_after_2yrs: 2025-11-17 11:01:01.952827
Timezones in DateTime can be used
future_date_after_2days: in the case
2023-11-20 where one might want to display time according to
11:01:01.952827
the timezone of a specific region. This can be done using the pytz module of Python. This module
serves the date-time conversion functionalities and helps users serving international client bases.
https://colab.research.google.com/drive/1usLrMDEMugNDAExYrSvBC8KBOUHkmUUO#printMode=true 7/7