![]() |
VOOZH | about |
In different regions of the world, different types of date formats are used and for that reason usually, programming languages provide a number of date formats for the developed to deal with. In Python, it is dealt with by using a liberty called DateTime. It consists of classes and methods that can be used to work with data and time values.
import datetimeTime values can be represented using the time class. The attributes for the time class include the hour, minute, second, and microsecond.
time(hour, minute, second, microsecond)Example 1:
Python3import datetime
tm = datetime.time(2, 25, 50, 13)
print(tm)
Output
02:25:50.000013Example 2:
There are ranges for the time attributes i.e for seconds we have the range between 0 to 59 and for nanoseconds, range is between 0 to 999999. If the range exceeds, the compiler shows a ValueError. The instance of time class consists of three instance attributes namely hour, minute, second, and microsecond. These are used to get specific information about the time.
Python3import datetime
tm = datetime.time(1, 50, 20, 133257)
print('Time tm is ',
tm.hour, ' hours ',
tm.minute, ' minutes ',
tm.second, ' seconds and ',
tm.microsecond, ' microseconds')
Output
Time tm is 1 hours 50 minutes 20 seconds and 133257 microseconds
The values for the calendar date can be represented via the date class. The date instance consists of attributes for the year, month, and day.
date(yyyy, mm, dd)Example 1:
Python3import datetime
date = datetime.date(2018, 5, 12)
print('Date date is ', date.day,
' day of ', date.month,
' of the year ', date.year)
Output
Date date is 12 day of 5 of the year 2018Example 2:
To get today's date names a method called today() is used and to get all the information in one object (today's information) ctime() method is used.
Python3import datetime
tday = datetime.date.today()
daytoday = tday.ctime()
print("The date today is ", tday)
print("The date info. is ", daytoday)
Output
The date today is 2020-01-30
The date info. is Thu Jan 30 00:00:00 2020
Conversion from string to date is many times needed while working with imported data sets from a CSV or when we take inputs from website forms. To do this, Python provides a method called strptime().
Syntax: datetime.strptime(string, format)
Parameters:
- string - The input string.
- format - This is of string type. i.e. the directives can be embedded in the format string.
Example:
Python3from datetime import datetime
print(datetime.strptime('5/5/2019',
'%d/%m/%Y'))
Output
2019-05-05 00:00:00Date and time are different from strings and thus many times it is important to convert the DateTime to string. For this, we use strftime() method.
Syntax: datetime.strftime(format, t)
Parameters:
- format - This is of string type. i.e. the directives can be embedded in the format string.
- t - the time to be formatted.
Example 1:
Python3import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%b %d %Y %H:%M:%S"))
Output
May 12 2018 02:25:50Example 2:
The same example can also be written in a different place by setting up the print() method.
Python3import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%H:%M:%S %b %d %Y"))
Output
02:25:50 May 12 2018 %H, %M and %S displays the hour, minutes and seconds respectively. %b, %d and %Y displays 3 characters of the month, day and year respectively. Other than the above example the frequently used character code List along with its functionality are:
import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%a"))
Output
Satimport datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%A"))
Output
Saturdayimport datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%B"))
Output
Mayimport datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%w"))
Output
6import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%m"))
Output
5import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%p"))
Output
PMimport datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("% y"))
Output
18import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("% f"))
Output
000013import datetime
x = datetime.datetime(2018, 5, 12, 2, 25, 50, 13)
print(x.strftime("%j"))
Output
132