我有一个日期时间对象,
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
答案 0 :(得分:3)
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
答案 1 :(得分:2)
使用datetime
模块:
import datetime
datetime.datetime.now().strftime('%a %B %d %H:%M:%S %Z %Y')
答案 2 :(得分:1)
我可以在这里找到问题的答案,原来Python文档提到了datetime对象与格式化字符串之间的这种转换:
https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior