Python DateTime
import datetime module in order to work with dates.
In the above output, it has year(2024), month(07) ,day(13), hour(11), minute(58), second(02) and millisecond(317731).
To create the date use datetime() class and it requires 3 parameters year, month and day , it can also include time but it is an optional parameter.
The datetime object use strftime() for formatting date objects to readable strings and it takes single parameter( format).
| Directive | Description | Example | | --- | --- | --- | | %a | Weekday, short version | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%a")) Output : Fri | | %A | Weekday, full version | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%A")) Output : Friday | | %w | Weekday as a number 0-6, 0 is Sunday | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%w")) Output : 5 | | %d | Day of month 01-31 | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%d")) Output : 12 | | %b | Month name, short version | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%b")) Output : Jul | | %B | Month name, full version | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%B")) Output : July | | %m | Month as a number 01-12 | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%m")) Output : 07 | | %y | Year, short version, without century | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%y")) Output : 24 | | %Y | Year, full version | import datetime d = datetime.datetime(2024,7,12) print(d.strftime("%Y")) Output : 2024 | | %H | Hour 00-23 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%H")) Output : 12 | | %I | Hour 00-12 | import datetime d = datetime.datetime(2024,7,12,14,15,55,316782) print(d.strftime("%I")) Output : 02 | | %p | AM/PM | import datetime d = datetime.datetime(2024,7,12,14,15,55,316782) print(d.strftime("%p")) Output : PM | | %M | Minute 00-59 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%M")) Output : 15 | | %S | Second 00-59 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%S")) Output : 55 | | %f | Microsecond 000000-999999 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%f")) Output : 316782 | | %Z | Timezone | CST | | %j | Day number of year 001-366 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%j")) Output : 194 | | %U | Week number of year, Sunday as the first day of week, 00-53 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%U")) Output : 27 | | %W | Week number of year, Monday as the first day of week, 00-53 | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%W")) Output : 28 | | %c | Local version of date and time | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%c")) Output : Fri Jul 12 14:15:55 2024 | | %C | Century | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%C")) Output : 20 | | %x | Local version of date | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%x")) Output : 07/12/24 | | %X | Local version of time | import datetime d = datetime.datetime(2024,7,12,12,15,55,316782) print(d.strftime("%X")) Output : 14:15:55 |
Subscribe to my newsletter
Read articles from bellam anu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by