У меня есть объект datetime,
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
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.
Пример взят из здесь
Я могу найти ответ на этот вопрос здесь, оказывается, в документации по Python упоминается этот вид преобразования между объектом datetime в форматированную строку:
https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
Используйте datetime
модуль:
import datetime
datetime.datetime.now().strftime('%a %B %d %H:%M:%S %Z %Y')
Похожие вопросы
Новые вопросы
python
Python - это многопарадигмальный, динамически типизированный, многоцелевой язык программирования. Он разработан для быстрого изучения, понимания и использования, а также для обеспечения чистого и единообразного синтаксиса. Обратите внимание, что Python 2 официально не поддерживается с 01.01.2020. Тем не менее, для вопросов о Python, связанных с версией, добавьте тег [python-2.7] или [python-3.x]. При использовании варианта Python (например, Jython, PyPy) или библиотеки (например, Pandas и NumPy) включите его в теги.