iampython2134
iampython2134

Reputation: 53

How to Remove MicroSeconds from DateTime

Hi i am using the library win32com.client to read emails from an outlook sharedmailbox and i get a value error:Microsecond must be in 0..999999. I tried formatting the "ReceivedDate" as "%Y-%D-%M %H:%M:%S" with no luck. do you know what else i can try?

import win32com.client

def readEmail():
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders.Item('SharedMailbox, TEST')
inbox = folder.Folders.Item('Inbox')
messages = inbox.Items

counter = 0
for message in messages:
    rcvd_dt = message.ReceivedTime
    if message.FlagRequest != 'Follow up' and str(rcvd_dt) >= '2020-06-01 00:00:00':
    counter  +=1
    print(counter)
    print(received_dt)

traceback:

Traceback (most recent call last):
File "C:/Users/TEST/PycharmProjects/TEST/Metrics.py", line 422, in <module>
main()
File "C:/Users/TEST/PycharmProjects/TEST/Metrics.py", line 409, in main
readEmail()
File "C:/Users/TEST/PycharmProjects/TEST/Metrics.py", line 89, in readEmail
rcvd_dt = message.ReceivedTime
File "C:\Program Files (x86)\Python37-32\lib\site- 
packages\win32com\client\dynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
ValueError: microsecond must be in 0..999999

What i tried:

rcvd_dt= datetime.strptime(str(rcvd_dt.split('.')[0], '%Y-%m-%d %H:%M:%S')

but get error:

valueerror: time data '2' does not match format %Y-%m-%d %H:%M:%S.%f'

if i try:

rcvd_dt= datetime.strptime(str(rcvd_dt.split('.')[0], '%Y-%m-%d %H:%M:%S.%f')

i get :

valueerror: time data '2020-06-16 08:53:56' does not match format %Y-%m-%d.%f' 

Upvotes: 1

Views: 508

Answers (2)

Ronald
Ronald

Reputation: 3325

I believe you can also extract year, month, ... like this:

rcvd_dt.year, rcvd_dt.month, rcvd_dt.day, ...

Upvotes: 0

zwjjoy
zwjjoy

Reputation: 370

'You must have 50 reputation to comment'

rcvd_dt= datetime.strptime(str(rcvd_dt.split('.')[0], '%Y-%m-%d %H:%M:%S')

You are on the right track with this. The error you are facing is occurred from rcvd_dt.split('.')[0]. There can be no '.' in the string to separate out the datetime string, and caused it to read '2' from the first index.

Upvotes: 2

Related Questions