Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove milliseconds from Datetime in TZ format

I have a datetime column that contains DateTime in TZ format like:

timestamp = 2021-04-01T04:37:16.528Z

I have to convert/truncate the milliseconds part such that datetime is:


as.POSIXct(timestamp, format="%Y-%m-%dT%H:%M:%OSZ", tz="GMT")

2021-04-01 04:37:16

But when I group by DateTime column for aggregations it still uses the timestamp in milliseconds format and the aggregations are not gettig implemented properly.

I want to truncate not just print the DateTime till seconds only.

like image 636
jongju Avatar asked Jan 23 '26 19:01

jongju


1 Answers

You could drop the miliseconds part from the string, and then convert:

as.POSIXct(gsub("\\.[0-9]+Z", "", timestamp), 
           format="%Y-%m-%dT%H:%M:%S", tz="GMT")
like image 170
Wimpel Avatar answered Jan 25 '26 09:01

Wimpel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!