0

I have following time stamp received as event to the lambda function where i need to extract the date from the timestamp

import datetime as dt

case1_time=2021-06-23T00:05:05-04:00
case2_time=2021-08-03T04:57:52.30-04:00

def get_date_from_ts(timestamp):
    extracted_date=dt.datetime.fromisoformat(timestamp)

#case 1
print(get_date_from_ts(case1_time))
## This will extract the date as = 2021-06-23

#case2
print(get_date_from_ts(case2_time))

ValueError: Invalid isoformat string 

i need to handle both case by the way.. can anyone help how to resolve this format issue?

adhi hari
  • 41
  • 1
  • 9

3 Answers3

0

You can take this as example:

Input:

from datetime import datetime

today_date = datetime.now().strftime("%d/%m/%Y")

print(today_date)

Output:

19/08/2021

Try to transform "case1_time=2021-06-23T00:05:05-04:00" before it is saved in a variable.

DaveMier88
  • 50
  • 5
0

How about simply extracting the date part from the timestamp?

def get_date_from_ts(timestamp):
    extracted_date=dt.datetime.fromisoformat(timestamp[:10])
    return extracted_date
Output
2021-06-23 00:00:00
2021-08-03 00:00:00
norie
  • 9,609
  • 2
  • 11
  • 18
0

Got a simplest way by spliting at certain point

#Convert to string
string_date_time=str(case1_time)

#split at specific location - T00:05:05-04:00
string_date_time.split('T')[0]
#output:
2021-06-23



adhi hari
  • 41
  • 1
  • 9