This question already has answers here:
R - Help in Converting factor to date (%m/%d/%Y %H:%M)
(4 answers)
Closed 4 years ago.
I have a date value "11/7/2016 11:51" which is currently in "mm/dd/yyyy hh:mm" format. I want to convert this date to "2016-11-07 11:51:00" i.e "yyyy-mm-dd hh:mm:ss" format using R language.
I would like to have any suggestions/help. Thanks in advance.. !!
Or mdy_hm from lubridate
lubridate::mdy_hm("11/7/2016 11:51")
[1] "2016-11-07 11:51:00 UTC"
lubridate will default to timezone UTC.
as.POSIXct will default to your (computer) timezone.
We can use as.POSIXct from base R
as.POSIXct( "11/7/2016 11:51" , format = "%m/%d/%Y %H:%M")
#[1] "2016-11-07 11:51:00 IST"
If we want to change the timezone, there is tz argument
Related
This question already has an answer here:
Milliseconds in POSIXct Class
(1 answer)
Closed 1 year ago.
I have a dataframe in r which consists of a column with the following DateTime format with character class.
dt<-2021-03-01 06:10:31.346
I need to convert it into the following format with datetime class.
2021-03-01 06:10:31
I have tried the following solution but it didn't work.
Df$Dt<- as.POSIXct(Df$Dt,format="%Y-%m-%dT%H:%M:%S")
The T is not needed as it was not present in the original string
as.POSIXct(dt, format="%Y-%m-%d %H:%M:%S")
#[1] "2021-03-01 06:10:31 CST"
If we want to take care of the microseconds
as.POSIXct(dt, format="%Y-%m-%d %I:%M:%OS")
Also, the format is not really needed here as the input is in the accepted format for as.POSIXct
as.POSIXct(dt)
#[1] "2021-03-01 06:10:31 CST"
data
dt <- '2021-03-01 06:10:31.346'
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"
This question already has an answer here:
How to convert a character string date to date class if day value is missing
(1 answer)
Closed 5 years ago.
I am trying to convert a character vector of dates (in the format: i.e. "Jan.1990") to a date vector (keeping a similar format: i.e. "Jan 1990" or "Jan.1990").
month_year <- ("Jan.1990", "Feb.1990", "Mar.1990", "Jan.1991", "Feb.1991", Mar. 1991")
I have tried using as.Date and various commands with the lubridate package but I either end up with an incorrect format or NAs.
I tried using as.Date:
df$month_year <- as.Date(df$month_year,format = "%b%Y")
This resulted in NAs.
I tried lubridate::parse_date_time2
parse_date_time2(tidy_labor$month_year, c("Jan.1990"), exact = TRUE, orders = "bY")
But the format came out as:
unknown timezone 'Jan.1990'unknown timezone 'Jan.1990' [1] "1990-01-01 GMT" "1990-01-01 GMT" "1990-01-01 GMT" "1990-01-01 GMT" "1990-02-01 GMT" "1990-02-01 GMT" "1990-02-01 GMT"
Any help would be much appreciated.
This question comes up a lot and the short answer is that dates require a day.
You could use zoo::as.yearmon:
library(zoo)
as.yearmon("Jan.1990", "%b.%Y")
Or you could assume that the day is always the first of the month, concatenate that to your values and convert to Date type.
This question already has an answer here:
Extract time from datatime in R
(1 answer)
Closed 6 years ago.
My dataset df has datetime column,
this column's format is YYYY-MM-DD HH:MM
I made new column that has only YYYY-MM-DD using
df$date <- factor(as.Date(df$datetime))
but I can't make column that has only time,
I want to make new column 'time' that has format HH:MM
Use strptime and strftime, the former convert character vectors to POSIXct and POSIXlt class( or date time class) and the latter does the opposite. With the format parameter, you can easily convert and format your datetime:
s = "2016-01-01 10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%H:%M")
# [1] "10:20"
strftime(strptime(s, "%Y-%m-%d %H:%M"), "%Y-%M-%d")
# [1] "2016-20-01"
I am trying to convert the date as factor to date using “as.date” function in R. I have the date in the following format
2008-01-01 02:30
I tried to use the following command :
as.Date(mydata$Date, format="%y-%m-%d %h:%mm")
Can somebody help me with this ? I was able to convert the format with no hour but getting difficulty with hour included.
Thank you.
Your format string is incorrect :
R> strptime("2008-01-01 02:30", format="%Y-%m-%d %H:%M")
# [1] "2008-01-01 02:30:00"
See ?strptime for the detailed values you can use to define a format.
Also note that as your string is in a standard format, you can also use directly as.POSIXlt :
R> as.POSIXlt("2008-01-01 02:30")
# [1] "2008-01-01 02:30:00"