Python datetime

Filed Under: Python

Python datetime

In this post, we will study about how to use the python datetime module. With datetime module, we can get information about datetime and format dates as well.
python datetime

Using datetime module

Let’s start working on python datetime module straightaway.

Python datetime now()

It is easy to print current time using datetime module. Let’s see the code snippet on how this can be done:


import time
import datetime

print("Time in seconds since the epoch: %s", time.time())
print("Current date and time: " , datetime.datetime.now())

Let’s see the output for this program:
python datetime now

At first, accessing a property inside datetime module which has the same name looks odd. You can call the now() function to print the date information, as shown above. Notice that datetime now() returns the date-time information in a user-friendly way and looks much better than we get from time module.

Python datetime format

Most of the times, single date format do not fulfill our needs in the application. We can format our code using the code snippet we show next:


import datetime

print("Formatted date : " , datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))

Let’s see the output for this program:
python datetime format
Note that here, the smaller m reflects the Month and capital M shows the minute part for time. This often leads to confusions and is a point to remember.

Python datetime example

We can do a lot more with datetime format like getting the present year, calculating weekday of the day, day of the year, etc. All of this is possible with single calls and we just need to use the strftime() function appropriately. Let’s see the code snippet on how this can be done:


import datetime

print("Present year: ", datetime.date.today().strftime("%Y"))
print("Month of year: ", datetime.date.today().strftime("%B"))
print("Week number of the year: ", datetime.date.today().strftime("%W"))
print("Weekday of the week: ", datetime.date.today().strftime("%w"))
print("Day of year: ", datetime.date.today().strftime("%j"))
print("Day of the month : ", datetime.date.today().strftime("%d"))
print("Day of week: ", datetime.date.today().strftime("%A"))

Let’s see the output for this program:
python datetime example
Now that was a lot of information which can be used.

Getting Weekday for a Date

It is also possible to get a weekday for a given date. This is sometimes needed in calendar-based applications or just for curiosity!

Let’s see the code snippet on how this can be done:


import datetime

birthday = datetime.date(1994,5, 20)  #year, month, day
print(birthday.strftime("%A"))

Let’s see the output for this program:
python date time day example

Python String to datetime

We can use strptime() function to get datetime object from String. Let’s see a short example for this.


import datetime

time_now = datetime.datetime.strptime("1/1/2018", "%m/%d/%Y")

print(time_now)
print(type(time_now))

The output of the above program is shown below.


2018-01-01 00:00:00
<class 'datetime.datetime'>

Conclusion

In this post, we saw how we can use the Python datetime module to provide date information and put it to use in our application. It’s a very useful module to provide date and time information in a user-friendly way. Also, there are many functions for formatting and getting specific information. So this module comes very handily in python application dealing with dates.

Reference: API Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages