I scraped data from Twitter and I got my date format as 2020-01-07T22:24:20.000Z
. I need to convert it to datetime format.
Asked
Active
Viewed 359 times
2

FObersteiner
- 22,500
- 8
- 42
- 72

dee dee
- 27
- 2
-
1See https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date for further information. – Camilo Martinez M. May 01 '21 at 00:12
3 Answers
3
We can convert a string to pandas datetime(Timestamp) type using pd.to_datetime method.
pd.to_datetime('2020-01-07T22:24:20.000Z')
Output
Timestamp('2020-01-07 22:24:20+0000', tz='UTC')
If you have a column which needs to be converted to datetime, then
Input
df = DataFrame({
'time':['2020-01-07T22:24:20.000Z', '2020-01-08T22:24:20.000Z']
})
time
0 2020-01-07T22:24:20.000Z
1 2020-01-08T22:24:20.000Z
Conversion
df['time'] = pd.to_datetime(df.time)
df
Output
time
0 2020-01-07 22:24:20+00:00
1 2020-01-08 22:24:20+00:00

Utsav
- 5,572
- 2
- 29
- 43
0
You could use the datetime
library
import datetime as dt
date = '2020-01-07T22:24:20.000Z'
formatted = dt.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.000Z')
Output is Out[25]: datetime.datetime(2020, 1, 7, 22, 24, 20)
Note that the decimals from the seconds and the characters T and Z where inserted in my strptime()
call

Joaquin Hoffmann
- 440
- 4
- 4
-
using a literal `Z` in the parsing directive leads to naive datetime (tzinfo not set to UTC) - which is wrong in my opinion since Z denotes UTC and Python treats naive datetime as *local time*, not UTC. – FObersteiner May 07 '21 at 19:54
-1
You need to parse the date first
function parseTwitterDate(aDate)
{
return new Date(Date.parse(aDate.replace(/( \+)/, ' UTC$1')));
//sample: Wed Mar 13 09:06:07 +0000 2013
}
Then you can convert it to time
console.log(parseTwitterDate('2020-01-07T22:24:20.000Z').getTime());
This should return the timestamp, This code for javascript and hope it will help you

Hussam Kurd
- 8,066
- 2
- 43
- 38