Skip to main content
 首页 » 编程设计

Python 日期格式 D M d H :i:s T Y

2025年05月04日15lovecherry

我有一个日期时间对象,

import time, datetime, pytz 
current_unixtime                            = time.time() 
current_date_milis_for_blibli               = int(round(current_unixtime * 1000)) 
current_datetime_object                     = datetime.datetime.fromtimestamp(current_unixtime, pytz.timezone('Asia/Jakarta')) 

我如何将它转换成:

 Mon May 16 14:07:15 WIB 2016  

或者在 PHP 中:

D M d H:i:s T Y 

我试过的写在下面,正如你所看到的,我似乎无法获得日和月的 3 个字符:

year    = current_datetime_object.year 
month   = current_datetime_object.month 
day     = current_datetime_object.day 
hour    = current_datetime_object.hour 
minute  = current_datetime_object.minute 
second  = current_datetime_object.second 

请您参考如下方法:

result = current_datetime_object.strftime("%a %b %d %H:%M:%S %Z %Y") 

您还可以通过更改括号中的值来指定输出。

示例基于 datetime.datetime(2013, 9, 30, 7, 6, 5)。

Code    Example    Meaning 
 
%a      Mon                          # Weekday as locale’s abbreviated name. 
%A      Monday                       # Weekday as locale’s full name. 
%w      1                            # Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 
%d      30                           # Day of the month as a zero-padded decimal number. 
%-d     30                           # Day of the month as a decimal number. (Platform specific) 
%b      Sep                          # Month as locale’s abbreviated name. 
%B      September                    # Month as locale’s full name. 
%m      9                            # Month as a zero-padded decimal number. 
%-m     9                            # Month as a decimal number. (Platform specific) 
%y      13                           # Year without century as a zero-padded decimal number. 
%Y      2013                         # Year with century as a decimal number. 
%H      7                            # Hour (24-hour clock) as a zero-padded decimal number. 
%-H     7                            # Hour (24-hour clock) as a decimal number. (Platform specific) 
%I      7                            # Hour (12-hour clock) as a zero-padded decimal number. 
%-I     7                            # Hour (12-hour clock) as a decimal number. (Platform specific) 
%p      AM                           # Locale’s equivalent of either AM or PM. 
%M      6                            # Minute as a zero-padded decimal number. 
%-M     6                            # Minute as a decimal number. (Platform specific) 
%S      5                            # Second as a zero-padded decimal number. 
%-S     5                            # Second as a decimal number. (Platform specific) 
%f      0                            # Microsecond as a decimal number, zero-padded on the left. 
%z                                   # UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). 
%Z                                   # Time zone name (empty string if the object is naive). 
%j      273                          # Day of the year as a zero-padded decimal number. 
%-j     273                          # Day of the year as a decimal number. (Platform specific) 
%U      39                           # 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. 
%W      39                           # 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. 
%c      Mon Sep 30 07:06:05 2013     # Locale’s appropriate date and time representation. 
%x      09/30/13                     # Locale’s appropriate date representation. 
%X      07:06:05                     # Locale’s appropriate time representation. 
%%      %                            # A literal '%' character. 

例子取自here