Date and Time Function

What is Time stamp?


In Python, Date and Time are data types not object

Python Date Time Module: It has classes to work on date and time. These classes have number of function to deal with data and time.

Date Time Module categorized in 6 part:

Date:

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.

from datetime import date
print(date(2022,11,23))

#get the current date
date.today()

#get the current year, month,date
date.today().year
date.today().day
date.today().month

Convert Date to string:

from datetime import date
today=date.today()
date.isoformat(today)

List of Date Class Methods

Function Name 

Description

ctime()Return a string representing the date
fromisocalendar()Returns a date corresponding to the ISO calendar
fromisoformat()Returns a date object from the string representation of the date
fromordinal()Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1
fromtimestamp()Returns a date object from the POSIX timestamp
isocalendar()Returns a tuple year, week, and weekday
isoformat()Returns the string representation of the date
isoweekday()Returns the day of the week as an integer where Monday is 1 and Sunday is 7
replace()Changes the value of the date object with the given parameter
strftime()Returns a string representation of the date with the given format
timetuple()Returns an object of type time.struct_time
today()Returns the current local date
toordinal()Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1
weekday()Returns the day of the week as integer where Monday is 0 and Sunday is 6
1.
from datetime import date
tod=date.today()
tod.ctime()

2.
The fromisocalendar() method transforms a date in the Gregorian calendar format to an equivalent date in the ISO calendar format. This date is defined by the year, the week number, and the day of the week.

3.
from datetime import date
tod=date.today()
date.fromisoformat('2022-02-01')

4.
from datetime import date
tod=date.today()
o=tod.toordinal()
date.fromordinal(o)

5.
from datetime import date
Timestamp = 1323456464;
date.fromtimestamp(Timestamp)

6.

from datetime import date
tod=date.today()
date.isocalendar(tod)

7.
from datetime import date
tod=date.today()
date.isoweekday(tod)

8.
from datetime import date
tod=date.today()
date.timetuple(tod)

9.
strftime()

from datetime import date
tod=date.today()
tod.strftime("%y-%m-%d")
print(tod.strftime("%B"))
tod.strftime("%W")



Get Date from TimeStamp:

from datetime import datetime
print(datetime.fromtimestamp(1887639468))

Time:

It represents a local time of the day which is independent of any particular day, This class can have the tzinfo object which represents the time zone of the given time. 

ex:

from datetime import time

mytime=time(12,4,5)

mytimem=time(minute=12)

print(mytimem,mytime)

Attributes provided by this Time class 

Attribute NameDescription
minMinimum possible representation of time
maxMaximum possible representation of time
resolutionThe minimum possible difference between time objects
hourThe range of hour must be between 0 and 24 (not including 24)
minuteThe range of minute must be between 0 and 60 (not including 60)
secondThe range of second must be between 0 and 60 (not including 60)
microsecondThe range of microsecond must be between 0 and 1000000 (not including 1000000)
tzinfoThe object containing timezone information
foldRepresents if the fold has occurred in the time or not

1. from datetime import time

print(time.min,time.max)

2.

from datetime import time

mytime=time(12,34,56)

print(mytime,mytime.minute,mytime.hour,mytime.second)

Time Class Functions

Function NameDescription
dst()Returns tzinfo.dst() is tzinfo is not None
fromisoformat()Returns a time object from the string representation of the time
isoformat()Returns the string representation of time from the time object
replace()Changes the value of the time object with the given parameter
strftime()Returns a string representation of the time with the given format
tzname()Returns tzinfo.tzname() is tzinfo is not None
utcoffset()Returns tzinfo.utcffsets() is tzinfo is not None


DateTime: It contains information date as well as time. it contain information regarding time zone as well.

Class Attributes of DateTime

Let’s see the attributes provided by this class:

Attribute Name

Description

min

The minimum representable DateTime

max

The maximum representable DateTime

resolution

The minimum possible difference between datetime objects

year

The range of year must be between MINYEAR and MAXYEAR

month

The range of month must be between 1 and 12

day

The range of days must be between 1 and the number of days in the given month of the given year

hour

The range of hours must be between 0 and 24 (not including 24)

minute

The range of minutes must be between 0 and 60 (not including 60)

second

The range of second must be between 0 and 60 (not including 60)

microsecond

The range of microseconds must be between 0 and 1000000 (not including 1000000)

tzinfo

The object containing timezone information

fold

Represents if the fold has occurred in the time or not

https://www.geeksforgeeks.org/python-datetime-module/

TimeDelta: It is used for calculating differences between dates and represents a duration. The difference can both be positive as well as negative.

from datetime import date,timedelta

mytime=date(12,3,5)

mytime=mytime-timedelta(days=5)

mytime

tzinfo:

tzinfo is an abstract base class. It cannot be instantiated directly. A concrete subclass has to derive it and implement the methods provided by this abstract class.

The instance of the tzinfo class can be passed to the constructors of the datetime and time objects. It finds its applications in situations such as the conversion of local time to UTC or to account for daylight saving time.


The methods available for implementation in tzinfo base class are :

  • utcoffset(self, dt): It return the offset of date time instance passed as an argument.
Offset: it refere to the timezone offset which denotes how many hours the time zone is head from the coordinated universal time.

from datetime import datetime,tzinfo,timedelta
import pytz
mytime=datetime.now(pytz.timezone('America/Mexico_City'))
mytime=mytime.utcoffset()
print(mytime)

  • dst(self, dt):
dst(): day light saving, it denotes advancing the clock 1 hour in summer time, so that darkness fall later according to clock.

from datetime import datetime,tzinfo,timedelta
import pytz
mytime=datetime.now(pytz.timezone('America/Mexico_City'))
#mytime=mytime.utcoffset()
mytimedst=mytime.dst()
print(mytimedst)

  • tzname(self, dt)
from datetime import datetime,tzinfo,timedelta
import pytz
mytime=datetime.now(pytz.timezone('America/Mexico_City'))
print(mytime.tzname())

Timezone: It is used when we have to display time according to the time of specific region.

from datetime import datetime,tzinfo,timedelta

from pytz import timezone

print(datetime.now(timezone('UTC')))

print(datetime.now(timezone('Asia/Kolkata')))

print(datetime.now(timezone('Europe/Kiev')))

convert time zone

from datetime import datetime,tzinfo,timedelta

from pytz import timezone

ut=datetime.now(timezone('UTC'))

now_asia = ut.astimezone(timezone('Europe/Kiev'))

print(now_asia,ut)