≡ Menu

Python: Get Today’s Current Date and Time

How do I find out the current date and time in Python? What is the module or function I need to use to get current time or date in Python programming language?

You can use time module (low level) which provides various time-related functions. However, this module is always available, not all functions are available on all platforms. Hence, you need to use the datetime (high level Object-oriented interface to dates and times) module in Python. It provides classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.

Getting current time in python using time module

The syntax is:

## 24 hour format ##
## 12 hour format ##


A simple program in python to get today’s date and time:

import time
print (time.strftime("%H:%M:%S"))
## 12 hour format ##
print (time.strftime("%I:%M:%S"))

Sample outputs:


To print current date use:

import time
## dd/mm/yyyy format
print (time.strftime("%d/%m/%Y"))

Sample outputs:


Getting locals date and time in Python

import time
now = time.strftime("%c")
## date and time representation
print "Current date & time " + time.strftime("%c")
## Only date representation
print "Current date "  + time.strftime("%x")
## Only time representation
print "Current time " + time.strftime("%X")
## Display current date and time from now variable 
print ("Current time %s"  % now )

Sample outputs:

Current date & time Sat Oct  5 00:04:59 2013
Current date 10/05/13
Current time 00:04:59
Current time Sat Oct  5 00:04:59 2013

Format strings

The following directives can be embedded in the format string:

%a Weekday name.
%A Full weekday name.
%b Abbreviated month name.
%B Full month name.
%c Appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%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 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) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Appropriate date representation.
%X Apropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal ‘%’ character.

Get the current date and time in Python using datetime module

The syntax is:

now = datetime.datetime.now()


import datetime
i = datetime.datetime.now()
print ("Current date & time = %s" % i)
print ("Date and time in ISO format = %s" % i.isoformat() )
print ("Current year = %s" %i.year)
print ("Current month = %s" %i.month)
print ("Current date (day) =  %s" %i.day)
print ("dd/mm/yyyy format =  %s/%s/%s" % (i.day, i.month, i.year) )
print ("Current hour = %s" %i.hour)
print ("Current minute = %s" %i.minute)
print ("Current second =  %s" %i.second)
print ("hh:mm:ss format = %s:%s:%s" % (i.hour, i.month, i.second) )

Sample outputs:

Current date & time = 2013-10-05 00:15:31.769049
Date and time in ISO format = 2013-10-05T00:15:31.769049
Current year = 2013
Current month = 10
Current date (day) =  5
dd/mm/yyyy format =  5/10/2013
Current hour = 0
Current minute = 15
Current second =  31
hh:mm:ss format = 0:10:31

You can use date.strftime(format) to get a string representing the date, controlled by an explicit format string (see above table):

from datetime import datetime
i = datetime.now()
print str(i)
print i.strftime('%Y/%m/%d %H:%M:%S')

Sample outputs:

2013-10-05 00:20:30.495228
2013/10/05 00:20:30

See python time and datetime module for more info.

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 15 comments… add one }
  • Seth Williams October 22, 2013, 3:53 am

    Hey Nixcraft, that’s an excellent tutorial on obtaining the current date and time. Thanks a lot for sharing

  • Miguel Mota November 28, 2013, 8:31 pm

    Thanks for this helpful post!

  • Christian Dipagel December 13, 2013, 5:33 am

    What a phenomenal tutorial! I stumbled upon it by accident while doing homework, solved a lot of problems I have had in the past! Thanks a ton!

  • Nathan January 28, 2014, 11:48 am

    very helpful post… i wondered on the internet but this is exactly what i was looking for!!!!
    great work . thanks a lot

  • Bing Soewarno February 7, 2014, 9:17 am

    Thanks, it is so helpful, many thanks again.

  • Jacob April 8, 2014, 6:59 am

    Thanks for the tutorial!
    Just one typo:

    print (“hh:mm:ss format = %s:%s:%s” % (i.hour, i.month, i.second) )

    I have a feeling that the second entry in the tuple should be i.minute.

    Just thought I’d mention this in case anyone copies the example code into their program, and is confused by the incorrect timestamps…

  • Mohan May 29, 2014, 5:58 am

    How to interface keyboard as raw input for GUI Calc……?

  • Anas Hart December 10, 2014, 8:02 pm

    nice explanation, however it only prints the time when you open the file, is there a way to make it so that the time updates?

    • Anthony Conejo May 13, 2016, 1:27 am

      I would use a while

  • Anonymous December 13, 2014, 9:33 am

    There is a typo in the datetime examples. The last format has i.month instead of i.minute.

  • Lavish gupta August 23, 2015, 4:43 am

    Really very helpful post

  • Imon November 2, 2015, 7:40 pm

    But how could I produce a code in which it records current time when ‘enter’ is pressed?
    Also how can I take an amount of time away from another.

  • Savva November 26, 2015, 2:25 pm

    Helpful note, thank y

  • ash ketchum September 19, 2016, 8:39 am

    nice meme bro

  • Jignesh Gor December 8, 2016, 9:40 pm

    It was really helpful. Thank you.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , ,