Home > Net >  how to convert pywintypes.datetime to different format
how to convert pywintypes.datetime to different format

Time:01-10

I have an object x which is as below

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'>

I want to convert above object to 2020-06-15 format and also to Year-Quarter format i.e. 2020 Q2

Is there any way to achieve this?

CodePudding user response:

Maybe this can help you....

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)}')

CodePudding user response:

you have methods available as you have for the datetime.datetime class, so you can use .year and .month attributes. Ex:

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