;

How to Get The Current Date And Time In Python


Tutorialsrack 29/03/2020 Programs Python

In this article, we will learn how to get the current date and time in python. And we will also format the date and time in various formats using the strftime() method.

There are various ways to get the current date and time in python. Here are some examples as follows:

Example 1: In this example, we will use now() method returns output in YYYY-MM-DD HH:MM:SS:MS format. And this method belongs to the Datetime Module, so we need to import this module.

Example 1
# Import Module
import datetime

Current_date_and_time = datetime.datetime.now()
print("\nCurrent Date and Time: ",Current_date_and_time)
# Output ==> Current Date and Time:  2020-03-29 16:05:44.405009

Example 2: In this example, we will use date.today() method for which we need to import date module from datetime module to get the current date.

Example 2
#Import Module
from datetime import date

Current_date_and_time = date.today()
print("\nToday's Date: ", Current_date_and_time)
# Output ==> Today's Date:  2020-03-29

Example 3: In this example, we used strftime() to create a string representing date and time in another format. Here are some examples of Formatted Date and Time as Follows:

Example 3
# Import module
import datetime

now = datetime.datetime.now()

print
print ("\nCurrent Date and Time using str method of datetime object: ",str(now))
# Output ==> Current date and time using str method of datetime object:  2020-03-29 15:40:54.501845

print ("\nCurrent Date and Time: ",now.strftime("%Y-%m-%d %H:%M:%S"))
# Output ==> Current Date and Time:  2020-03-29 16:05:44

print ("\nCurrent Date: ",now.strftime("%Y/%m/%d"))
# Output ==> Current Date:  2020/03/29

print ("\nCurrent Time in 24hour format: ",now.strftime("%H:%M:%S"))
# Output ==> Current Time in 24hour format:  16:05:44

print ("\nCurrent Time in 12hour format: ",now.strftime("%I:%M:%S %p"))
# Output ==> Current Time in 12hour format:  04:05:44 PM

print ("\nCurrent Date format(dayname, month name day, year): ",now.strftime("%a, %b %d, %Y"))
# Output ==> Current Date format(dayname, month name day, year):  Sun, Mar 29, 2020

print ("\nCurrent date and time using strftime:",now.strftime("%Y-%m-%d %H:%M"))
# Output ==> Current date and time using strftime: 2020-03-29 16:05

print ("\nCurrent date and time using isoformat:", now.isoformat())
# Output ==> Current date and time using isoformat: 2020-03-29T16:05:44.405009

print ("\nCurrent date and time using instance attributes:")
print ("\nCurrent year: ", now.year)
# Output ==> Current year:  2020

print ("\nCurrent month: ", now.month)
#Output ==> Current month:  3

print ("\nCurrent day: ", now.day)
# Output ==> Current day:  29

print ("\nCurrent hour: ", now.hour)
# Output ==> Current hour:  16

print ("\nCurrent minute: ", now.minute)
# Output ==> Current minute:  5

print ("\nCurrent second: ", now.second)
# Output ==> Current second:  44

print ("\nCurrent microsecond: ", now.microsecond)
# Output ==> Current microsecond:  405009 

Format strings to get the current date and time in Python

Below is the list of directives that can be used to format the date and time output in Python.

 

Directive

Description

Example

%a

Weekday, short version

Wed

%A

Weekday, full version

Wednesday

%w

Weekday as a number 0-6, 0 is Sunday

3

%d

Day of month 01-31

31

%b

Month name, short version

Dec

%B

Month name, full version

December

%m

Month as a number 01-12

12

%y

Year, short version, without century

18

%Y

Year, full version

2018

%H

Hour 00-23

17

%I

Hour 00-12

05

%p

AM/PM

PM

%M

Minute 00-59

41

%S

Second 00-59

08

%f

Microsecond 000000-999999

548513

%z

UTC offset

+0100

%Z

Timezone

CST

%j

Day number of year 001-366

365

%U

Week number of year, Sunday as the first day of week, 00-53

52

%W

Week number of year, Monday as the first day of week, 00-53

52

%c

Local version of date and time

Mon Dec 31 17:41:00 2018

%x

Local version of date

12/31/18

%X

A local version of time

17:41:00

%%

A % character

%

I hope this article will help you to understand how to get the current date and time in python. 

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags