時間格式,在所有程式語言都是必學的。
- strftime : datetime object 轉換成String
- strptime : datetime string 轉換成object
顯示目前時間範例:
import datetime
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
轉換字串為日期物件:
datetime_object = datetime.strptime(last_modified_time, '%Y-%m-%d %H:%M:%S')
取出 timestamp:
datetime.timestamp(datetime_object)
指派 timestamp 到 datetime object:
obj = datetime.timestamp(datetime_object)
For reference, here are the codes used in the mini-language:
%aWeekday as locale’s abbreviated name.%AWeekday as locale’s full name.%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.%dDay of the month as a zero-padded decimal number.%bMonth as locale’s abbreviated name.%BMonth as locale’s full name.%mMonth as a zero-padded decimal number. 01, …, 12%yYear without century as a zero-padded decimal number. 00, …, 99%YYear with century as a decimal number. 1970, 1988, 2001, 2013%HHour (24-hour clock) as a zero-padded decimal number. 00, …, 23%IHour (12-hour clock) as a zero-padded decimal number. 01, …, 12%pLocale’s equivalent of either AM or PM.%MMinute as a zero-padded decimal number. 00, …, 59%SSecond as a zero-padded decimal number. 00, …, 59%fMicrosecond as a decimal number, zero-padded on the left. 000000, …, 999999%zUTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030%ZTime zone name (empty if naive), UTC, EST, CST%jDay of the year as a zero-padded decimal number. 001, …, 366%UWeek number of the year (Sunday is the first) as a zero padded decimal number.%WWeek number of the year (Monday is first) as a decimal number.%cLocale’s appropriate date and time representation.%xLocale’s appropriate date representation.%XLocale’s appropriate time representation.%%A literal ‘%’ character.