How one goes about to reformat date to CCYYMMDD HH:MM:SS TZ ? (the TZ is optional)
Here is related post but I would need the solution within R.
Here are my dates, which I would need to reformat.
library(lubridate)
startdate <- as.Date("2015-01-01")
week.dates <- seq(startdate, by="1 week", length.out=12)
dat.week <- week.dates[wday(week.dates) != 1 & wday(week.dates) != 7]
biz.week <- format(as.POSIXct(as.Date(dat.week)), tz="America/Los_Angeles",usetz=TRUE)
EDIT: Specifically, needed format is: 'YYYYMMDD{SPACE}hh:mm:ss[{SPACE}TMZ]'
Just try:
strftime(dat.week,format="%Y%m%d %H:%M %Z")
You can also use format or as.character instead of strftime.
I think you also requested the seconds, so here's a minor modification:
strftime(dat.week,format="%C%y%m%d %H:%M:%S %Z")
Related
This looks like trivial issue but I couldn't make it work. I need simply convert midnight into POSIXct format but also with hours, minutes and seconds, just like that:
nextDay_t <- strptime(paste0(as.character(Sys.Date() + 1)," 00:00:00"), format = "%Y-%m-%d %H:%M:%S")
nextDay_t <- format(nextDay_t, "%Y-%m-%d %H:%M:%S")
nextDay_t <- as.POSIXct(nextDay_t, format='%Y-%m-%d %H:%M:%S', tz="EST")
But still have only "2018-12-11 EST" instead of "2018-12-11 00:00:00 EST". Is there anything I'm missing in my code?
Your code seems on the right track. The hour/minute/second components are in fact still there after calling strptime. They just do not show up automatically by default when inspecting the object.
You may try the following call to format, which includes these components, as well as the time zone (%Z):
nextDay_t <- strptime(paste0(as.character(Sys.Date() + 1)," 00:00:00"), format = "%Y-%m-%d %H:%M:%S")
nextDay_t
format(nextDay_t,"%Y/%m/%d %H:%M:%S %Z") # include hour/minute/second and time zone
[1] "2018-12-11 CET"
[1] "2018/12/11 00:00:00 CET"
I have a column in my dataframe as datetime (factor) with the values as "15-10-2017 16:41:00".
I wanted this data to be converted as "2017-10-15 16:41:00".
When i try to convert this, I'm getting the timezone also as output.
I tried using tz="", usetz=F but no use.
Any suggestions ?
Code:
as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
[1] "2017-10-15 16:41:00 IST"
From the help page of as.POSIXlt:
"" is the current time zone
which is the default.
That's why it does not work. You could remove the timezone information this way, and it will not show while printing:
my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
my_datetime$zone <- NULL
my_datetime
but I don't understand why you would want to do that. You should convert to GMT if you don't want to worry about the timezone. Also lubridate package has a nice force_tz function if you have to force some specific timezones.
If you are ok storing the datetime as a character instead of as a POSIXlt, then you can use strftime():
my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strftime(my_datetime)
I do it like this:
strip.tz <- function(dt) {
fmt <- "%Y-%m-%d %H:%M:%S"
strptime(strftime(dt, format = fmt, tz=""), format = fmt, tz="UTC")
}
and you would use it like this:
my_datetime <- as.POSIXct("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strip.tz(my_datetime)
I have data input coming in the form of d/m/y time eg: "3/27/2017 1:20:15 AM" and I want to just have d/m/y to display 3/27/2017.
I have seen different techniques on how to achieve this and have tried 3 different types however I recieve NA as the new output and for the later I get the "Error in charToDate(x) :
character string is not in a standard unambiguous format"
error.
Here is an my example code:
df <- data.frame(Date = "3/27/2017 1:20:15 AM")
df$Date <- as.POSIXct(as.character(df$Date), format = "%d/%m/%Y")
df$Date <- as.Date(df$Date, "%d/%m/%Y")
df$Date <- format(as.Date(df$Date), "%d/%m/%Y")
Looks like you want format = %m/%d/%Y instead of %d/%m/%Y.
If specialized date formats are still giving you problems with the time after that, you can first use strsplit() to break up the character string on spaces, then grab the first item to get just the date.
date <- "3/27/2017 1:20:15 AM"
date <- strsplit(date," ")
date <- unlist(date)[1]
date <- as.POSIXct(date,format="%m/%d/%Y")
You need to provide complete format string in order to identify it as a date-time.
In your case it would be something like this:
as.Date("3/27/2017 1:20:15 AM", format = "%m/%d/%Y %I:%M:%S %p")
I have this data frame which gives me Date and Time columns. I am trying to combine these 2 columns but strptime is returning NA. i want to understand why is it happening?
x <- data.frame(date = "1/2/2007", time = "00:00:02")
y <- strptime(paste(x$date,x$time,sep = " "), format = "%b/%d/%y %H:%M:%S")
We need %m and %Y in place of %b and %y (%b - Abbreviated month name in the current locale on this platform. %y - Year without century (00–99)).
strptime(paste(x$date,x$time,sep = " "), "%m/%d/%Y %H:%M:%S")
#[1] "2007-01-02 00:00:02 IST"
For understanding the format, it is better to check ?strptime
Or we can use mdy_hms from lubridate
library(lubridate)
with(x, mdy_hms(paste(date, time)))
#[1] "2007-01-02 00:00:02 UTC"
I need to turn one date format into another with RStudio, since for lubridate and other date related functions a standard unambiguous format is needed for further work. I've included a few examples and informations below:
Example-Dataset:
Function,HiredDate,FiredDate
Waitress,16-06-01 12:40:02,16-06-13 11:43:12
Chef,16-04-17 15:00:59,16-04-18 15:00:59
Current Date Format (POSIXlt) of HiredDate and FiredDate:
"%y-%m-%d %H:%M:%S"
What I want the Date Format of HireDate and FiredDate to be:
"%Y-%m-%d %H:%M:%S" / 2016-06-01 12:40:02
or
"%Y/%m/%d %H:%M:%S" / 2016/06/01 12:40:02
In principle, you can convert date and time for example using the strftime function:
d <- "2016-06-01 12:40:02"
strftime(d, format="%Y/%m/%d %H:%M:%S")
[1] "2016/06/01 12:40:02"
In your case, the year is causing trouble:
d <- "16-06-01 12:40:02"
strftime(d, format="%Y/%m/%d %H:%M:%S")
[1] "0016/06/01 12:40:02"
As Dave2e suggested, the two digit year can be read by %y:
strftime(d, format="%y/%m/%d %H:%M:%S")
[1] "16/06/01 12:40:02"
Assuming that your data comes from the 20st and 21st century, you can paste a 19 or 20 in front of the HireDate and FireDate:
current <- 16
prefixHire <- ifelse(substr(data$HireDate, 1, 2)<=currentYear,20,19)
prefixFire <- ifelse(substr(data$FireDate, 1, 2)<=currentYear,20,19)
data$HireDate = paste(prefixHire, data$HireDate, sep="")
data$FireDate = paste(prefixFire, data$FireDate, sep="")
The code generates a prefix by assuming that any date from a year greater than the current ('16) is actually from the 20th century. The prefix is then pasted to HireDate and FireDate.