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