from datetime import datetime
now = datetime.now()
print(now)
Code - %a : Weekday as locale’s abbreviated name.
Example:
Sun, Mon, …, Sat (en_US);
So, Mo, …, Sa (de_DE)
day = now.strftime("%a")
print("Weekday as locale’s abbreviated name:", day)
Code - %A : Weekday as locale’s full name
Example:
Sunday, Monday, …, Saturday (en_US);
Sonntag, Montag, …, Samstag (de_DE)
day = now.strftime("%A")
print("Weekday as locale’s full name:", day)
Code - %w: Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
Example:
0, 1, …, 6
day = now.strftime("%w")
print("Weekday as a decimal number:", day)
Code - %d: Day of the month as a zero-padded decimal number.
Example:
01, 02, …, 31
day = now.strftime("%d")
print("Day of the month as a zero-padded decimal number:", day)
Code - %b: Month as locale’s abbreviated name.
Example:
Jan, Feb, …, Dec (en_US);
Jan, Feb, …, Dez (de_DE)
day = now.strftime("%b")
print("Month as locale's abbreviated name:", day)
Code - %B: Month as locale’s full name.
Example:
January, February, …, December (en_US);
Januar, Februar, …, Dezember (de_DE)
day = now.strftime("%B")
print("Month as locale's full Name:", day)
Code - %m: Month as a zero-padded decimal number.
Example:
01, 02, …, 12
day = now.strftime("%m")
print("Month as a zero-padded decimal number:", day)
Code - %y: Year without century as a zero-padded decimal number.
Example:
00, 01, …, 99
day = now.strftime("%y")
print("Year without century as a zero-padded decimal number:", day)
Code - %Y: Year with century as a decimal number.
Example:
0001, 0002, …, 2013, 2014, …, 9998, 9999
day = now.strftime("%Y")
print("Year with century as a decimal number:", day)
Code - %H: Hour (24-hour clock) as a zero-padded decimal number.
Example:
00, 01, …, 23
day = now.strftime("%H")
print("Hour as a zero-padded decimal number:", day)
Code - %I: Hour (12-hour clock) as a zero-padded decimal number.
Example:
01, 02, …, 12
day = now.strftime("%I")
print("Hour as a zero-paddes decimal number:", day)
Code - %p: Locale’s equivalent of either AM or PM.
Example:
AM, PM (en_US);
am, pm (de_DE)
day = now.strftime("%p")
print("Locale's equivalent of either AM Or PM:", day)
Code - %M: Minute as a zero-padded decimal number.
Example:
00, 01, …, 59
day = now.strftime("%M")
print("Minute as a zero-padded decimal number:", day)
Code - %S: Second as a zero-padded decimal number.
Example:
00, 01, …, 59
day = now.strftime("%S")
print("Second as a zero-padded decimal number:", day)
Code - %f: Microsecond as a decimal number, zero-padded on the left.
Example:
000000, 000001, …, 999999
day = now.strftime("%f")
print("Microsecond as a decimal number, zero-padded on the left:", day)
Code - %z: UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
Example:
(empty), +0000, -0400, +1030, +063415, -030712.345216
import time
from datetime import time, tzinfo, timedelta
class TZ1(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=1)
def dst(self, dt):
return timedelta(0)
def tzname(self,dt):
return "+01:00"
def __repr__(self):
return f"{self.__class__.__name__}()"
t = time(10, 8, 55, tzinfo=TZ1())
day = t.strftime("%z")
print("UTC offset in the form ±HHMM[SS[.ffffff]]:", day)
Code - %Z: Time zone name (empty string if the object is naive).
Example:
(empty), UTC, EST, CST
import time
from datetime import time, tzinfo, timedelta
class TZ1(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=1)
def dst(self, dt):
return timedelta(0)
def tzname(self,dt):
return "+01:00"
def __repr__(self):
return f"{self.__class__.__name__}()"
t = time(10, 8, 55, tzinfo=TZ1())
day = t.strftime("%Z")
print("Time zone name:", day)
Code - %j: Day of the year as a zero-padded decimal number.
Example:
001, 002, …, 366
day = now.strftime("%j")
print("Day of the year as a zero-padded decimal number:", day)
Code - %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.
Example:
00, 01, …, 53
day = now.strftime("%U")
print("Week number of the year as a zero-padded decimal number:", day)
Code - %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.
Example:
00, 01, …, 53
day = now.strftime("%W")
print("Week number of the year as a zero-padded decimal number:", day)
Code - %c: Locale’s appropriate date and time representation.
Example:
Tue Aug 16 21:30:00 1988 (en_US);
Di 16 Aug 21:30:00 1988 (de_DE)
day = now.strftime("%c")
print("Locale's appropriate date and time representation:", day)
Code - %x: Locale’s appropriate date representation.
Example:
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
day = now.strftime("%x")
print("Locale's appropriate date representation:", day)
Code - %X: Locale’s appropriate time representation.
Example:
21:30:00 (en_US);
21:30:00 (de_DE)
day = now.strftime("%X")
print("Locale's appropriate time representation:", day)
Code - %%: A literal '%' character.
Example:
%
day = now.strftime("%%")
print("A literal '%' character:", day)
Several additional directives not required by the C89 standard are included for convenience.
These parameters all correspond to ISO 8601 date values. These may not be available on all platforms when used with the strftime() method.
The ISO 8601 year and ISO 8601 week directives are not interchangeable with the year and week number directives above.
Calling strptime() with incomplete or ambiguous ISO 8601 directives will raise a ValueError.
Code - %G: ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V).
Example:
0001, 0002, …, 2013, 2014, …, 9998, 9999
day = now.strftime("%G")
print("ISO 8601 year with century representing the year that contains the greater part of the ISO week:", day)
Code - %u: ISO 8601 weekday as a decimal number where 1 is Monday.
Example:
1, 2, …, 7
from datetime import datetime
now = datetime.now()
day = now.strftime("%u")
print("ISO 8601 weekday as a decimal number where 1 is Monday:", day)
Code - %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.
Example:
01, 02, …, 53
day = now.strftime("%V")
print("ISO 8601 week as a decimal number with Monday as the first day of the week.:", day)
New in version 3.6: %G, %u and %V were added.