Format Codes for the Time Format String

就算我在寫python,也是會去查 ni.com 官網的 Format Codes for the Time Format String

https://www.ni.com/docs/en-US/bundle/labview/page/glang/codes_for_time_format_str.html

The Format Date/Time String function calculates the date and time and formats the output string according to the following format codes.

Format Codes for the Time Format String Table

Format Code Value
<%a> abbreviated weekday name (for example Wed)
<%A> full weekday name (for example Wednesday)
<%b> abbreviated month name (for example Jun)
<%B> full month name (for example June)
<%c> locale-specific default date and time
<%d> day of month (01–31)
<%H> hour (24-hour clock) (00–23)
<%I> hour (12-hour clock) (01–12)
<%j> day number of the year (001–366)
<%m> month number (01–12)
<%M> minute (00–59)
<%p> AM or PM flag
<%S> second (00–59)
<%u> fractional seconds with precision
<%U> week number of the year (00–53), with the first Sunday as the first day of week one; 00 represents the first week
<%w> weekday as a decimal number (0–6), with 0 representing Sunday
<%W> week number of the year (00–53), with the first Monday as the first day of week one; 00 represents the first week
<%x> locale-specific date
<%.1x> long date format
<%.2x> abbreviated long date format
<%X> locale-specific time
<%y> year within century (00–99); when you scan the numbers, numbers (00–68) represent years in the twenty-first century (2000–2068) and numbers (69–99) represent years in the twentieth century (1969–1999)
<%Y> year, including the century (for example, 1997)
<%z> difference between locale time and universal time (HH:MM:SS)
<%Z> time zone name or abbreviation, depending on the operating system locale settings

Example

import datetime, pytz
# Get the current time in Taipei
# 工控界夥伴都在世界各地,這邊嚴格指定要列出台北的時間
tz = pytz.timezone('Asia/Taipei')
current_time = datetime.datetime.now(tz)

print(f"Current time in Taipei: {current_time:%H:%M:%S}")
1個讚