1import datetime
2
3today = datetime.datetime.now()
4date_time = today.strftime("%m/%d/%Y, %H:%M:%S")
5print("date and time:",date_time)
1from datetime import datetime as d
2date = d.now()
3print(date.strftime("%Y-%m-%d %H:%M:%S"))
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. (%)
1from datetime import date
2f_date = date(2014, 7, 2)
3l_date = date(2014, 7, 11)
4delta = l_date - f_date
5print(delta.days)
6
7
1strftime() and strptime() Format Codes
2The following is a list of all the format codes that the 1989 C standard requires, and these work on all platforms with a standard C implementation.
3
4%a : Weekday as locale’s abbreviated name. #Sun, Mon, …, Sat (en_US); So, Mo, …, Sa (de_DE)
5%A : Weekday as locale’s full name. # Sunday, Monday, …, Saturday (en_US) Sonntag, Montag, …, Samstag (de_DE)
6%w : Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
7
8%d : Day of the month as a zero-padded decimal number.
9
10%b : Month as locale’s abbreviated name. # Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE)
11%B : Month as locale’s full name. # January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE)
12%m : Month as a zero-padded decimal number.
13
14%y : Year without century as a zero-padded decimal number.
15%Y : Year with century as a decimal number. # 0001, 0002, …, 2013, 2014, …, 9998, 9999
16
17%H : Hour (24-hour clock) as a zero-padded decimal number.
18%I : Hour (12-hour clock) as a zero-padded decimal number.
19
20%p : Locale’s equivalent of either AM or PM.
21
22%M : Minute as a zero-padded decimal number.
23%S : Second as a zero-padded decimal number.
24%f : Microsecond as a decimal number, zero-padded on the left. # 000000, 000001, …, 999999
25
26%z : UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). # (empty), +0000, -0400, +1030, +063415, -030712.345216
27%Z : Time zone name (empty string if the object is naive). # (empty), UTC, GMT
28
29%j : Day of the year as a zero-padded decimal number. #001, 002, …, 366
30
31%U : Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
32%W : Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
33
34%c : Locale’s appropriate date and time representation. #Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE)
35%x : Locale’s appropriate date representation. # 08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE)
36%X : Locale’s appropriate time representation. # 21:30:00 (en_US); 21:30:00 (de_DE)
37
38%% : A literal '%' character.
39
40%G : ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V). 0001, 0002, …, 2013, 2014, …, 9998, 9999
41%u : ISO 8601 weekday as a decimal number where 1 is Monday.
42%V : ISO 8601 week as a decimal number with Monday as the first day of the week. Week 01 is the week containing Jan 4.