我有一個物件x如下
x = '2020-06-15 00:00:00 00:00'
print(x)
2020-06-15 00:00:00 00:00
print(type(x))
<class 'pywintypes.datetime'>
我想將上面的物件轉換為2020-06-15格式,也轉換為Year-Quarter formatie2020 Q2
有什么辦法可以做到這一點?
uj5u.com熱心網友回復:
也許這可以幫助你......
from math import ceil
x = '2020-06-15 00:00:00 00:00'
date, time = x.split()
yy, mm, dd = date.split('-')
print(f'{yy} - Q{ceil(int(mm)/3)}')
uj5u.com熱心網友回復:
您可以使用與datetime.datetime 類一樣的方法,因此您可以使用 .year 和 .month 屬性。前任:
import math
# --- Windows-specific! ---
import pywintypes
import win32timezone
t = pywintypes.Time(1592179200).astimezone(win32timezone.TimeZoneInfo('UTC'))
print(t)
# 2020-06-15 00:00:00 00:00
print(f'{t.year}-Q{math.ceil(int(t.month)/3)}')
# 2020-Q2
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410910.html
標籤:
上一篇:為什么我無法使用datetime欄位從Appsmith查詢資料到MongoDB?
下一篇:SQLSTATE[22007]:日期時間格式無效:1292日期時間值不正確:“fotostudio”列的“28-01-202212:00”。“transaction_details”
