Datetime Library
Datetime open library of Python,we can fetch datetime or any specific thing with the help of this module.
- Know current Date and time
- Print specific year month or day
- Creating Date objects
>>> import datetime >>> x=datetime.datetime.now() >>> print(x) 2020-09-05 19:20:19.703489
>>> import datetime >>> x=datetime.datetime.now() >>> x.year 2020 >>> x.month 9 >>> x.day 5
Syntax : datetime(year,month,day)
>>> import datetime >>> x=datetime.datetime(2009,12,22) >>> x datetime.datetime(2009, 12, 22, 0, 0) >>>
The Strftime() method
Strftim() method is used for the make date object in a more readable string.
Directive | Description |
%a | Return weekend,less details |
%A | Return weekend,full details |
%w | Return weekend,as number |
%b | Return Month name,sort version |
%B | Return Month name,full version |
%d | Return day of month |
%m | Return month(Number) |
%y | Return year,sort version |
%Y | Return year,full version |
%H | Return Hour from 0 to 24 |
%I | Return Hour from 0 to 12 |
%p | Return AM and PM |
%M | Return Minutes 0 to 59 |
%S | Return Second 0 to 59 |
%f | Return Microsecond 00000-999999 |
%z | Return UTC offset |
%Z | Return time zone |
%j | Return Days of year |
%U | Return weekend number of year(Starting from monday) |
%c | Return local number datetime |
%x | Return local number date |
%X | Return local number time |
- 1 Use of %a
- Use of %A
- Use of %w
- use of %b
- use of %B
- use of %d
- use of %m
- use of %y
- use of %Y
- use of %H
- use of %I
- use of %p
- use of %M
- use of %S
- use of %f
- use of %j
- use of %u
- use of %c
- use of %x
- use of %X
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%a")) Sat >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%A")) Saturday >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%w")) 6 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%b")) Sep >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%B")) September >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%d")) 05 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%m")) 09 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%y")) 20 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%Y")) 2020 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%H")) 19 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%I")) 07 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%y")) PM >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%M")) 55
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%S")) 40 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%f")) 971489 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%j")) 249 >>>
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%u")) 6
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%c")) Sat Sep 5 20:03:44 2020
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%x")) 09/05/20
>>> import datetime >>> details=datetime.datetime.now() >>> print(details.strftime("%X")) 20:03:44 >>>