1The program below converts a datetime object containing current date and time to different string formats.
2
3Code:
4
5from datetime import datetime
6
7now = datetime.now() # current date and time
8
9year = now.strftime("%Y")
10print("year:", year)
11
12month = now.strftime("%m")
13print("month:", month)
14
15day = now.strftime("%d")
16print("day:", day)
17
18time = now.strftime("%H:%M:%S")
19print("time:", time)
20
21date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
22print("date and time:",date_time)
23
24Output after run the code:
25year: 2020
26month: 03
27day: 31
28time: 04:59:31
29date and time: 03/31/2020, 04:59:31
30
31Here, year, day, time and date_time are strings, whereas now is a datetime object.
1| Directive | Meaning | Example |
2|-----------|------------------------------------------------------------------------------------------|
3|%a | Abbreviated weekday name. | Sun, Mon, .. |
4|%A | Full weekday name. | Sunday, Monday, ... |
5|%w | Weekday as a decimal number. | 0, 1, ..., 6 |
6|%d | Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
7|%-d | Day of the month as a decimal number. | 1, 2, ..., 30 |
8|%b | Abbreviated month name. | Jan, Feb, ..., Dec |
9|%B | Full month name. | January, February, ... |
10|%m | Month as a zero-padded decimal number. | 01, 02, ..., 12 |
11|%-m | Month as a decimal number. | 1, 2, ..., 12 |
12|%y | Year without century as a zero-padded decimal number. | 00, 01, ..., 99 |
13|%-y | Year without century as a decimal number. | 0, 1, ..., 99 |
14|%Y | Year with century as a decimal number. | 2013, 2019 etc. |
15|%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
16|%-H | Hour (24-hour clock) as a decimal number. | 0, 1, ..., 23 |
17|%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
18|%-I | Hour (12-hour clock) as a decimal number. | 1, 2, ... 12 |
19|%p | Locale’s AM or PM. | AM, PM |
20|%M | Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
21|%-M | Minute as a decimal number. | 0, 1, ..., 59 |
22|%S | Second as a zero-padded decimal number. | 00, 01, ..., 59 |
23|%-S | Second as a decimal number. | 0, 1, ..., 59 |
24|%f | Microsecond as a decimal number, zero-padded on the left. | 000000 - 999999 |
25|%z | UTC offset in the form +HHMM or -HHMM. | |
26|%Z | Time zone name. | |
27|%j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
28|%-j | Day of the year as a decimal number. 1, 2, ..., 366 | |
29|%U | Week number of the year (Sunday as the first day of the week). | 00, 01, ..., 53 |
30|%W | Week number of the year (Monday as the first day of the week). | 00, 01, ..., 53 |
31|%c | Locale’s appropriate date and time representation. | Mon Sep 30 07:06:05 2013|
32|%x | Locale’s appropriate date representation. | 09/30/13 |
33|%X | Locale’s appropriate time representation. | 07:06:05 |
34|%% | A literal '%' character. | % |
35
1%a - Abbreviated weekday name. (Sun, Mon, ...)
2%A - Full weekday name. (Sunday, Monday, ...)
3%w - Weekday as a decimal number. (0, 1, ..., 6)
4%d - Day of the month as a zero-padded decimal. (01, 02, ..., 31)
5%-d - Day of the month as a decimal number. (1, 2, ..., 30)
6%b - Abbreviated month name. (Jan, Feb, ..., Dec)
7%B - Full month name. (January, February, ...)
8%m - Month as a zero-padded decimal number. (01, 02, ..., 12)
9%-m - Month as a decimal number. (1, 2, ..., 12)
10%y - Year without century as a zero-padded decimal number. (00, 01, ..., 99)
11%-y - Year without century as a decimal number. (0, 1, ..., 99)
12%Y - Year with century as a decimal number. (2013, 2019 etc.)
13%H - Hour (24-hour clock) as a zero-padded decimal number. (00, 01, ..., 23)
14%-H - Hour (24-hour clock) as a decimal number. (0, 1, ..., 23)
15%I - Hour (12-hour clock) as a zero-padded decimal number. (01, 02, ..., 12)
16%-I - Hour (12-hour clock) as a decimal number. (1, 2, ... 12)
17%p - Locale’s AM or PM. (AM, PM)
18%M - Minute as a zero-padded decimal number. (00, 01, ..., 59)
19%-M - Minute as a decimal number. (0, 1, ..., 59)
20%S - Second as a zero-padded decimal number. (00, 01, ..., 59)
21%-S - Second as a decimal number. (0, 1, ..., 59)
22%f - Microsecond as a decimal number, zero-padded on the left. (000000 - 999999)
23%z - UTC offset in the form +HHMM or -HHMM.
24%Z - Time zone name.
25%j - Day of the year as a zero-padded decimal number. (001, 002, ..., 366)
26%-j - Day of the year as a decimal number. (1, 2, ..., 366)
27%U - Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. (00, 01, ..., 53)
28%W - Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. (00, 01, ..., 53)
29%c - Locale’s appropriate date and time representation. (Mon Sep 30 07:06:05 2013)
30%x - Locale’s appropriate date representation. (09/30/13)
31%X - Locale’s appropriate time representation. (07:06:05)
32%% - A literal '%' character. (%)
1import datetime
2print(datetime.datetime.now()) #datetime.datetime.now() is the syntax
1import datetime
2
3x = datetime.datetime(2018, 9, 15)
4
5print(x.strftime("%b %d %Y %H:%M:%S"))
6