How to Convert Time Stamp String "2014-07-20T05:11:49.988Z" into Posixt in R

How to convert time stamp string 2014-07-20T05:11:49.988Z into POSIXt in R?

The "Z" is shorthand for UTC. You can parse this in base R with

x <- as.POSIXct("2014-07-20T05:11:49.998Z", 
format="%Y-%m-%dT%H:%M:%OSZ", tz="GMT")

Note that you generally either use POSIXct or POSIXlt rather than POSIXt directly (both have POSIXt as a base class)

Retrieving day and time (in minutes) from character timestamp

  1. As noted in comments, use tz="UTC", otherwise the "Z" == UTC (zulu) information gets lost, also see this answer.

  2. If time is exactly midnight, the output is omitted.

as.POSIXct('2022-03-01T00:00:00Z', tz="UTC", "%Y-%m-%dT%H:%M:%OSZ")
# [1] "2022-03-01 UTC"

as.POSIXct('2022-03-01T11:11:11Z', tz="UTC", "%Y-%m-%dT%H:%M:%OSZ")
# [1] "2022-03-01 11:11:11 UTC"

  1. You have now read in the time correctly and got "POSIXct" class. To achieve a different output from it, you need to format it to character with the desired format using strftime which allows to specify the correct timezone tz=, e.g. "CET".

strftime(as.POSIXct('2022-03-01T00:00:00Z', tz="UTC", "%Y-%m-%dT%H:%M:%OSZ"),
'%F %R', tz='CET')
# [1] "2022-03-01 01:00"

strftime(as.POSIXct('2022-03-01T11:11:11Z', tz="UTC", "%Y-%m-%dT%H:%M:%OSZ"),
'%F %R', tz='CET')
# [1] "2022-03-01 12:11"

Or if I understand "day in numbers and the time in minutes correctly, you maybe want this:

strftime(as.POSIXct('2022-03-01T11:11:11Z', tz="UTC", "%Y-%m-%dT%H:%M:%OSZ"),
'%d %M', tz='CET')
# [1] "01 11"

Type help('strftime') or short ?strftime into the R console and read the help page for possible in/output options.

How do you convert time stamp that has AM/PM at the end of the string?

Read ?strptime. %p, which only works with %I, not %H. Your time format is also incorrect. Your times are separated by ".", not ":".

as.POSIXct("19-Jun-13 06.00.00.00 PM", format="%d-%b-%y %I.%M.%OS %p")

How to format this obscure character vector into datetime (or POSIX) objects in R?

lubridate's ymd_hms seems to work.

options(digits.secs=6)
lubridate::ymd_hms(times)

#[1] "2020-04-15 03:30:05.197 UTC" "2020-04-15 03:30:05.366 UTC"
# "2020-04-15 03:30:05.529 UTC"

extracting hour and minute from character column in r

We can use the convenient functions in lubridate to convert the character column to DateTime and extract the hour and minute with format

library(lubridate)
v1 <- ymd_hms("2016-05-28T05:53:31.042Z")
format(v1, "%H:%M")
#[1] "05:53"

Or using only base R

format(as.POSIXct("2016-05-28T05:53:31.042z", format = "%Y-%m-%dT%H:%M:%S"),  "%H:%M")
#[1] "05:53"

Other options include with gsub

gsub(".*T|:\\d+\\..*", "", "2016-05-28T05:53:31.042z")
#[1] "05:53"

Convert character date with timestamp to class POSIXct POSIXt

With lubridate, it can be dmy_hms

library(data.table)
library(lubridate)
DT2[, study_date := dmy_hms(study_date)]

Or using as.POSIXct

DT2[, study_date := as.POSIXct(study_date, format = '%d%b%Y:%H:%M:%S')]

NA result in converting string to POSIXct date time in R

This might be related to Daylight savings in your local timezone, as.POSIXct uses local timezone by default. Try to use timezone as UTC.

as.POSIXct("20210328 02:00:00", format = "%Y%m%d %H:%M:%S", tz = 'UTC')
#[1] "2021-03-28 02:00:00 UTC"

You can also use lubridate::ymd_hms which uses UTC timezone by default.

lubridate::ymd_hms("20210328 02:00:00")


Related Topics



Leave a reply



Submit