-1

there are date strings like 'Apr 21, 2021, 7:43:51 AM GMT+5:30' (downloaded from google spreadsheet into CSV).

What are these date formats? How to parse them into a timestamp?

Update

I have a function:

def to_timestamp(s):
    datetime_object = datetime.strptime(s, '%b %d, %Y, %I:%M:%S %p')
    return datetime_object.timestamp()

but I have no idea how to process GMT+5:30

martineau
  • 119,623
  • 25
  • 170
  • 301
BAE
  • 8,550
  • 22
  • 88
  • 171
  • Does this answer your question? [Converting string into datetime](https://stackoverflow.com/questions/466345/converting-string-into-datetime) – Woodford Jul 12 '21 at 21:56
  • 1
    Have you tried pandas.to_datetime() with the format parameter? – pythonweb Jul 12 '21 at 21:56
  • @Woodford I have no idea how to parse `GMT+5:30`. Any idea? – BAE Jul 12 '21 at 22:07
  • you could try the `Cell Formatting` section in the Google Spreadsheet before exporting as CSV to change the date format to your desired style. – shripal mehta Jul 12 '21 at 22:25
  • https://stackoverflow.com/questions/1101508/how-to-parse-dates-with-0400-timezone-string-in-python – Woodford Jul 12 '21 at 22:27

1 Answers1

1

Patch the timezone format:

import re

def to_timestamp(s):
    s = re.sub(r'([+-])(\d{1}):', '\g<1>0\g<2>:', s)
    datetime_object = datetime.strptime(s, '%b %d, %Y, %I:%M:%S %p %Z%z')
    return datetime_object.timestamp()

s = 'Apr 21, 2021, 7:43:51 AM GMT+5:30'
d = to_timestamp(s)
>>> d
1618971231.0
Corralien
  • 109,409
  • 8
  • 28
  • 52