Formatting Date/Time in R [duplicate] - r

This question already has an answer here:
converting datetime string to POSIXct date/time format in R
(1 answer)
Closed 2 years ago.
Using R, how do I transform the date and time from here, into a "standard" date/time string format?
2020-12-30T03:32:30.000Z

Use ?strptime
strptime("2020-12-30T03:32:30.000Z", "%Y-%m-%dT%H:%M:%S")
# [1] "2020-12-30 03:32:30 CET"

You can also use:
#Code
as.POSIXct(gsub('T|Z',' ','2020-12-30T03:32:30.000Z'),'%Y-%m-%d %H:%M:%S.%OS')
Output:
[1] "2020-12-30 03:32:30 GMT"

Related

How convert numeric value to date in R (same as Short date in excel)? [duplicate]

This question already has answers here:
How to convert Excel date format to proper date in R
(5 answers)
Closed 2 years ago.
I want to convert numeric date format to date. I.E. I have value 44109, but want to convert it to 2020-10-06. How is it possible?
Try something like below
> as.Date(44109,origin = "1900-01-01")
[1] "2020-10-07"
this works as well
x <- as.POSIXct(44109 * (60*60*24), origin="1899-12-30", tz="GMT")
"2020-10-05 GMT"

Convert string to Date in R: month abbreviated "dd-mmm-YYYY" [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 4 years ago.
I have a vector of strings which I need to convert to dates. This only works for some of the dates which are in the format of "dd-mmm-YYYY".
Example: this works: strptime("21-Sep-2017", format = "%d-%b-%Y")
This does not work and returns NA: strptime("21-Dec-2017", format = "%d-%b-%Y")
What am I doing wrong or not seeing?
This is because your locale is probably one where December is not abbreviated as Dec. Without changing your session settings, you could simply do
lubridate::parse_date_time("21-Dec-2017", orders = "d-b-Y", locale = "us")
[1] "2017-12-21 UTC"

Converting from POSIXct to character in R [duplicate]

This question already has an answer here:
Convert date to character in a data frame
(1 answer)
Closed 5 years ago.
I have a POSIXct datetime column in a file.
$date
[1] "POSIXct" "POSIXt"
"2005-10-13 14:13:08 UTC"
I would like to convert it to character column.
Any ideas?
Just cast your POSIXCT object to character, e.g.
date <- as.POSIXct("20-Jan-2018 19:06:08.314", format="%d-%b-%Y %H:%M:%OS")
date.str <- as.character(date)
date.str
[1] "2018-01-20 19:06:08"

Convert string to data format [duplicate]

This question already has an answer here:
R convert character vector values to Date values
(1 answer)
Closed 7 years ago.
I have a large number of strings of the following format:
a <- "19260701"
I would like to convert these do the following date format:
1926-07-01
when I try:
as.Date(a, "%Y-%m-%d")
I get
NA
You need to put the format of a in the function as.Date() :
a <- "19260701"
as.Date(a,format="%Y%m%d")
[1] "1926-07-01"
a<- "19260701"
library(lubridate)
ymd(a)
[1] "1926-07-01 UTC"

Convert RFC 3339 timestamp (R language) [duplicate]

This question already has an answer here:
Trouble with date format using the function as.POSIXct in R
(1 answer)
Closed 8 years ago.
I have a data frame with RFC 3339 format ("2012-04-12T19:02:32Z") and I need to turn into a date type R, I found as.POSIXct function but can not use it properly.
Try this:
R> as.POSIXct("2012-04-12T19:02:32Z", format="%Y-%m-%dT%H:%M:%SZ")
[1] "2012-04-12 19:02:32 CDT"
R>
The arguments for the format string are detailed in the help page.

Resources