I have a date and time as this:
"2013-09-05 0:00am"
I need to convert this to POSIX and I tried this:
as.POSIXct(c("2013-09-01 0:00am"), format="%Y-%m-%d %I:%M%p")
I am getting an NA output, any ideas why?
Because there's no such thing as "00:00 AM". If you're using an AM/PM indicator, then the only possible values for hours are 1-12. If you want to use hours 00-24, then you can't use an AM/PM indicator.
I think there is no 0:00am.
Try
as.POSIXct(c("2013-09-01 1:00am"), format="%Y-%m-%d %I:%M%p",tz="America/New_York")
and it works.
Related
I want to format the date in ISO 8601 format using lubridate. At the moment the code I have parses the date almost the way I want. The only thing I want to change is to have a colon in the time zone offset. My code at the moment:
dateTime <- str_match(fileName, dateTimeRegex)[2] %>% ymd_hms() %>% strftime(format = "%y-%m-%dT%H:%M:%S%z", tz = "UTC")
Sample output:
"19-09-26T10:45:00+0000"
Expected output:
"19-09-26T10:45:00+00:00"
Is there a simple way to do it, without parsing this manually? %z creates 0000, but I need a colon there
From Wikipedia (emphasis mine):
The UTC offset is the difference in hours and minutes from Coordinated Universal Time (UTC, or GMT) for a particular place and date. It is generally shown in the format ±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being described is one hour ahead of UTC (such as the time in Berlin during the winter), the UTC offset would be "+01:00", "+0100", or simply "+01".
HH:MM is just one way to format time offsets, the others being HHMM and HH, so your output conforms to ISO 8601.
We can use regex to achieve your desired output. Using sub
x <- "19-09-26T10:45:00+0000"
sub("(.*\\+)(\\d{2})(\\d{2})", "\\1\\2:\\3", x)
#[1] "19-09-26T10:45:00+00:00"
I obtained a time string looking like this:
201902041502, containing year, month, day, hour and minute.
Now I want to reformat this string into the german date-time format like this: 04.02.2019 15:02.
I've already tried as.Date and as.POSIXct but it doesnt work and I want to avoid adding seconds to get POSIXct to work.
Thanks in advance! Cheers
You can use strptime to convert the data to a POSIXlt object,
x <- "201902041502"
xd <- strptime(x,"%Y%m%d%H%M")
# [1] "2019-02-04 15:02:00 CET"
and then use strftime to produce your desired format:
strftime(xd, "%d.%m.%Y %H:%M")
# [1] "04.02.2019 15:02"
What could be the problem? I don't seem to get why this had to be NA.
as.Date("jan2012", format="%b%Y")
[1] NA
I have also used strptime function and it is the same thing. I have been using this functions but I don't know they are not working this morning. Any insight as to why this is will be useful.
The Date include day also. So, we need to paste with a day i.e. 01
as.Date(paste("jan2012", "01"), format="%b%Y%d")
#[1] "2012-01-01"
"jan2012" isn't a date, it's a month. You need to prefix the day you want, e.g.
as.Date(paste0("01", "jan2012"), format = "%d%b%Y")
I want to split up a Timestamp from Excel into year and julian day. I know, duplicate question, but combining everything I have found from other questions is not helping me.
The timestamp is formatted 1/13/2011 13:55 . So, I wanted to tell R to recognize this as a time variable. I have hours and minutes so I tried as.POSIXct and as.POSIXlt. These didn't work. I tried adding strptime --
as.POSIXct(strptime(df$TIMESTAMP, "%d/%m/%Y %H:%M%S"))
I just got NAs.
Once I got R to recognize it as a date, I was going to use lubridate like day(df$Date).
It seems as though you have month and day reversed
1/13/2011 13:55
with
"%d/%m/%Y %H:%M%S"
corresponds to the 1st day of the 13th month, which is probably why you're getting NAs. This seems to work for me:
a <- "01/13/2011 13:55"
t <- strptime(a, "%m/%d/%Y %H:%M")
t
"2011-01-13 13:55:00"
I have a time column in the format of 00:00 00:30 01:00.
I want to subset to certain hours but I don't understand how to convert it to the right format
for the date i used as.date() is there someting similar?
i am sorry i got it i used the strptime and strftime functions like so:
strftime(strptime(x, format="%H:%M"),"%H")