Date time is important part of the application development. Date is used in different format to show, save, or compare events and process. Date and time have different presentation formats which may be change according to countries and localization settings. In this tutorial we will look different usage examples of datetime operations.
Import Datetime Library
Python datetime functions are provided by the library named
datetime and in order to use date time functions we need to import this library like below.
from datetime import datetime
Date and Time
While using pythons datetime functions, libraries and data structures we will involve with two basic data structures date and time. Date part or object is used to hold date. Time part or object is used to hold time. We can use these separately without providing other part.
Current date or simply now represents date and time we are currently. We will use
now function in order to get current date and time.
now is provided by
now provide current date and time like below.
As we can see we print the current time and date into console and then we have set current date and time into variable named
now . We see that date time returns following values
Datetime From String
In previous example we have set date by getting current date and time from
now function. But in some situations we may need to set date and time manually for different date and times.We will provide date and time values as string. We will use
datetime function with the related date time format.
In this example we will provide values as
Every location in the world have different time but in order to make things more properly time zones are created. Time zones are used to synchronize some area time to the same time. The time zone may change according to location. We can get time zone information with
tzinfo command like below.
Datetime To Timestamp
We can print given date and time information the format we want. For example if we want to only print year and month but do not want to print day information we can provide related format with
strptime function. We will use formatters which is standardized with ISO 8601 to change output. For more information about formatters look next chapter.
format="%Y-%m-%d %H:%M" datetime.strftime(datetime.now(),format) format="%Y-%m %H:%M" datetime.strftime(datetime.now(),format) format="%H:%M" datetime.strftime(datetime.now(),format)
We can use following formatters
- %a Locale’s abbreviated weekday name.
- %A Locale’s full weekday name.
- %b Locale’s abbreviated month name.
- %B Locale’s full month name.
- %c Locale’s appropriate date and time representation.
- %d Day of the month as a decimal number [01,31].
- %f Microsecond as a decimal number [0,999999], zero-padded on the left
- %H Hour (24-hour clock) as a decimal number [00,23].
- %I Hour (12-hour clock) as a decimal number [01,12].
- %j Day of the year as a decimal number [001,366].
- %m Month as a decimal number [01,12].
- %M Minute as a decimal number [00,59].
- %p Locale’s equivalent of either AM or PM.
- %S Second as a decimal number [00,61].
- %U Week number of the year (Sunday as the first day of the week)
- %w Weekday as a decimal number [0(Sunday),6].
- %W Week number of the year (Monday as the first day of the week)
- %x Locale’s appropriate date representation.
- %X Locale’s appropriate time representation.
- %y Year without century as a decimal number [00,99].
- %Y Year with century as a decimal number.
- %z UTC offset in the form +HHMM or -HHMM.
- %Z Time zone name (empty string if the object is naive).
- %% A literal ‘%’ character.
Another requested feature is comparing date in each other. We can use normal logical comparison operators like
= . In the following example we will compare two dates and print some text according to comparison result.
t1=datetime.now() t2=datetime.now() if(t1<t2): print("t1 is lower than t2")
Date can be also subtracted from each other. This is very useful feature to find the interval between two dates as years, months, days, hours, minutes, seconds.
t1=datetime.now() t2=datetime.now() t2-t1
Time difference or delta between
t2 is only 3 seconds as we can see.
1 thought on “Python Date Time Functions With Examples”