[Python] 時間格式轉換

Posted in :

時間格式,在所有程式語言都是必學的。

  • 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:

  • %a Weekday as locale’s abbreviated name.
  • %A Weekday as locale’s full name.
  • %w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
  • %d Day of the month as a zero-padded decimal number.
  • %b Month as locale’s abbreviated name.
  • %B Month as locale’s full name.
  • %m Month as a zero-padded decimal number. 01, …, 12
  • %y Year without century as a zero-padded decimal number. 00, …, 99
  • %Y Year with century as a decimal number. 1970, 1988, 2001, 2013
  • %H Hour (24-hour clock) as a zero-padded decimal number. 00, …, 23
  • %I Hour (12-hour clock) as a zero-padded decimal number. 01, …, 12
  • %p Locale’s equivalent of either AM or PM.
  • %M Minute as a zero-padded decimal number. 00, …, 59
  • %S Second as a zero-padded decimal number. 00, …, 59
  • %f Microsecond as a decimal number, zero-padded on the left. 000000, …, 999999
  • %z UTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030
  • %Z Time zone name (empty if naive), UTC, EST, CST
  • %j Day of the year as a zero-padded decimal number. 001, …, 366
  • %U Week number of the year (Sunday is the first) as a zero padded decimal number.
  • %W Week number of the year (Monday is first) as a decimal number.
  • %c Locale’s appropriate date and time representation.
  • %x Locale’s appropriate date representation.
  • %X Locale’s appropriate time representation.
  • %% A literal ‘%’ character.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *