Example 1: Convert One String to Datetime
Here we are going to take a string as input and convert it to DateTime.
R
# consider a string string = "2021-11-21 4:5:23" # convert string to datetime final = as.POSIXct (string, format= "%Y-%m-%d %H:%M:%S" , tz= "UTC" ) # display print (final) # get the type class (final) |
Output:
[1] "2021-11-21 04:05:23 UTC" [1] "POSIXct" "POSIXt"
How to Convert String to Datetime in R?
In this article, we will discuss how to convert String to Datetime in R Programming Language. We can convert string to DateTime by using the POSIXct function
Syntax: as.POSIXct(string, format=”%Y-%m-%d %H:%M:%S”, tz=”UTC”)
where
- string is the input string
- format represents the datetime format
- tz specifies local time zone