Datetime Library
- Know current Date and time
>>> import datetime >>> x=datetime.datetime.now() >>> print(x) 2020-09-05 19:20:19.703489
- Print specific year month or day
>>> import datetime >>> x=datetime.datetime.now() >>> x.year 2020 >>> x.month 9 >>> x.day 5
- Creating Date objects
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
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%a"))
Sat
>>>
- Use of %A
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%A"))
Saturday
>>>
- Use of %w
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%w"))
6
>>>
- use of %b
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%b"))
Sep
>>>
- use of %B
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%B"))
September
>>>
- use of %d
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%d"))
05
>>>
- use of %m
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%m"))
09
>>>
- use of %y
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%y"))
20
>>>
- use of %Y
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%Y"))
2020
>>>
- use of %H
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%H"))
19
>>>
- use of %I
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%I"))
07
>>>
- use of %p
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%y"))
PM
>>>
- use of %M
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%M"))
55
- use of %S
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%S"))
40
>>>
- use of %f
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%f"))
971489
>>>
- use of %j
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%j"))
249
>>>
- use of %u
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%u"))
6
- use of %c
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%c"))
Sat Sep 5 20:03:44 2020
- use of %x
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%x"))
09/05/20
- use of %X
>>> import datetime
>>> details=datetime.datetime.now()
>>> print(details.strftime("%X"))
20:03:44
>>>