× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Python Dates


A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.
Example
Import the datetime module and display the current date:
import datetime
x = datetime.datetime.now()
print(x)

Date Output
When we execute the code from the example above the result will be:
2022-12-07 11:26:07.711930
The date contains year, month, day, hour, minute, second, and microsecond.
The datetime module has many methods to return information about the date object.
Here are a few examples, you will learn more about them later in this chapter:

Example
Return the year and name of weekday:
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))

Creating Date Objects
To create a date, we can use the datetime() class (constructor) of the datetime module.
The datetime() class requires three parameters to create a date: year, month, day.

Example
    
Create a date object:
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).

The strftime() Method
The datetime object has a method for formatting date objects into readable strings.
The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:

Example
Display the name of the month:

import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))

A reference of all the legal format codes:
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 24
%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 23
%Y Year, full version 2023
%H Hour 00-23 14
% Division x%y
%I Hour 00-12 2
%p AM/PM PM
%M Minute 00-59 24
%S Second 00-59 24
%f Microsecond 000000-999999 563467
%z UTC offset +0100
%Z Timezone IST
%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 Sat Dec 31 17:41:00 2022
%C Century 20
%x Local version of date 12/31/22
%X Local version of time 17:41:00
%% A % character %
%G ISO 8601 year 2023
%u ISO 8601 weekday (1-7 1
%V ISO 8601 weeknumber (01-53) 01

Weekday, short version
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%a"))

if __name__=="__main__":
    myfun()
Output:
Weekday, full version
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%A"))


if __name__=="__main__":
    myfun()
Output:
Weekday as a number 0-6, 0 is Sunday
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%w"))



if __name__=="__main__":
    myfun()
Output:
Day of month 01-31
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%d"))



if __name__=="__main__":
    myfun()
Output:
Month name, short version
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%b"))




if __name__=="__main__":
    myfun()
Output:
Month name, full version
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%B"))





if __name__=="__main__":
    myfun()
Output:
Month as a number 01-12
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%m"))






if __name__=="__main__":
    myfun()
Output:
Year, short version, without century
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%y"))

if __name__=="__main__":
    myfun()
Output:
Year, full version
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%Y"))

if __name__=="__main__":
    myfun()
Output:
Hour 00-23
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%H"))


if __name__=="__main__":
    myfun()
Output:
Hour 00-12
import datetime
def myfun():
   x = datetime.datetime.now()
   print(x.strftime("%I"))

if __name__=="__main__":
    myfun()
Output:
AM/PM
import datetime
def myfun():
   x = datetime.datetime.now()
   print(x.strftime("%p"))

if __name__=="__main__":
    myfun()
Output:
Minute 00-59
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%M"))

if __name__=="__main__":
    myfun()
Output:
Second 00-59
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%S"))


if __name__=="__main__":
    myfun()
Output:
Microsecond 000000-999999
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%f"))


if __name__=="__main__":
    myfun()
Output:
Day number of year 001-366
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%j"))



if __name__=="__main__":
    myfun()
Output:
Week number of year, Sunday as the first day of week, 00-53
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%U"))

if __name__=="__main__":
    myfun()
Output:
Week number of year, Monday as the first day of week, 00-53
import datetime
def myfun():
    x = datetime.datetime(2018, 5, 31)
    print(x.strftime("%W"))

if __name__=="__main__":
    myfun()
Output:
Local version of date and time
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%c"))

if __name__=="__main__":
    myfun()
Output:
Century
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%C"))

if __name__=="__main__":
    myfun()
Output:
Local version of date
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%x"))

if __name__=="__main__":
    myfun()
Output:
Local version of time
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%X"))


if __name__=="__main__":
    myfun()
Output:
A % character
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%%"))

if __name__=="__main__":
    myfun()
Output:
ISO 8601 year
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%G"))


if __name__=="__main__":
    myfun()
Output:
ISO 8601 weekday (1-7)
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%u"))



if __name__=="__main__":
    myfun()
Output:
ISO 8601 weeknumber (01-53)
import datetime
def myfun():
    x = datetime.datetime.now()
    print(x.strftime("%V"))


if __name__=="__main__":
    myfun()
Output: