1from datetime import datetime as d
2date = d.now()
3print(date.strftime("%Y-%m-%d %H:%M:%S"))
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 as d
2time = d.datetime.now()
3time = time.strftime("%Y-%m-%d %H:%M:%S")
4 #year-#month-#date #hour:#minure:#second
5print(time)
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.
1import datetime
2now = datetime.datetime.now()
3print(now.year, now.month, now.day, now.hour, now.minute, now.second)