Date and Time modules
In Python, a datetime is one of the python modules. It provides the classes for manipulating date and time in simpler and complex ways.
To import the datetime module is shown in below;
In python, the time module provides the functions that work with date and its conversions.
We can import date from datetime module as follows:
Functions of date module
The following are some of the functions of date module.
|>>> date.__name__ #Prints the name of module
‘date(year, month, day) –> date object’
1. date.today( ) It displays today’s date.
|>>> date.today( )
datetime.date(2018, 8, 3)
2. datetime.date.today( ) It displays current year, month and date.
datetime.date(2018, 8, 3)
3.datetime.date.min( ) It returns the earliest representable date.
datetime.date(1, 1, 1)
4. datetime.date.max( ) It is similar to min(); max returns the latest representable date.
datetime.date(9999, 12, 31)
5.datetime.date(year,month,day) The date function with three different attributes.
6.replace( ) We can update any or all of those values by using replace().
datetime.date(2018, 7, 21)
7 __str__( ) It displays the current local date as a string.
|>>> d.__str__( )
8.ctime( ) It displays the current local date in a string format.
‘Sat Jul 21 00:00:00 2018’
In python, the time module provides the functions that work with time and its conversions.
To import time module as:
If you want the details of time module then give as follows;
Functions of time module
The following are some of the functions of time module.
1.time.asctime( ) This accepts a time-tuple and returns a readable 24-character string.
‘Fri Aug 3 16:56:05 2018’
2. time.clock( ) This returns the current CPU time as a floating-point number of seconds. To measure computational costs of different approaches, the value of time.clock is more useful than that of time.time( ).
|>>> time.clock( )
3.time.ctime([secs]): It is similar to asctime(localtime(secs)) and without arguments is like asctime( ).
|>>> time.ctime( )
‘Fri Aug 3 17:03:15 2018’
4.time.gmtime([secs]): This accepts an instant expressed in seconds since the epoch and returns a time-tuple t with the UTC time.
Note : t.tm_isdst is always 0.
|>>> time.time( )
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=3, tm_hour=11, tm_min=34, tm_sec=25, tm_wday=4, tm_yday=215, tm_isdst=0)
5 . time.sleep(secs) Suspends the calling thread for secs seconds.
‘Fri Aug 3 17:14:19 2018’
start:Fri Aug 3 17:14:41 2018
end:Fri Aug 3 17:15:21 2018
6.time.time( ) This returns the current time instant, a floating-point number of seconds since the epoch.
7.timetuple( ) It returns a tuple of attributes for the current local time.
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=202, tm_isdst=-1)
1.Write a python program to know certain details of your bornday.
2.Another example program on datetime module.
|Time in seconds since the epoch: 1533299183.585952
Current date and time: 2018-08-03 17:56:23.601552
Or like this: 18-08-03-17-56
Current year: 2018
Month of year: August
Week number of the year: 31
Weekday of the week: 5
Day of year: 215
Day of the month : 03
Day of week: Friday
3. A python program on Date operations.
|Today’s Date: 08/03/18
Today is : Friday
Current Month is: August
You can apply the following directives in the format, as per your need.
- %a – abbreviated day of week
- %A – full day of week
- %b – abbreviated name of month
- %B – full name of month
- %c – preferred date and time representation
- %C – century number (the year divided by 100; range 00 to 99)
- %d – day of month (1 to 31)
- %D – the same as %m/%d/%y
- %e – day of month (1 to 31)
- %g – like %G, but without the century
- %G – 4-digit year corresponding to the ISO week number (ref. %V).
- %h – same as %b
- %H – hour; using a 24-hr clock (0 to 23)
- %I – hour; using a 12-hr clock (1 to 12)
- %j – day of year (1 to 366)
- %m – month (1 to 12)
- %M – minute
- %n – newline character
- %p – AM/PM according to given time value
- %r – time in AM/PM representation
- %R – time in 24-hr representation
- %S – second
- %t – tab
- %T – current time; equal to %H:%M:%S
- %u – day of week as a number (1 to 7); Monday=1
- %U – week of year, beginning with the first Sunday as the first day of the first week
- %V – The ISO 8601 week of year (1 to 53); week 1 is the first with at least 4 days in the current year, and with Monday as the first day of the week.
- %W – week of year; beginning with the first Monday as the first day of the first week.
- %w – day of week as a decimal; Sunday=0
- %x – the preferred date representation without the time
- %X – the preferred time representation without the date
- %y – year without century (00 to 99)
- %Y – year including century
- %Z or %z – time zone/name/abbreviation
- %% – a % character