How to turn PM/AM time stamp am/pm - r

I have a dataset with a column where date and time is stored.
The data I have is:
03/17/2020 09:30:00 PM
I want to convert AM/PM to a 24hour format.
My attempt was using this:
as.POSIXct(df$Date, format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
When I run this with the whole dataset, the majority of dates turns into "NA".
Why is this happening? I am really confused.

Using lubridate:
x <- "03/17/2020 09:30:00 PM"
lubridate::mdy_hms(x)
[1] "2020-03-17 21:30:00 UTC"
Using as.Posixct: note that you need the month / day convention, not the day/month:
as.POSIXct(x, format="%m/%d/%Y %I:%M:%S %p", tz = "UTC")
[1] "2020-03-17 21:30:00 UTC"

Related

Convert time and Date to 24hs

I have one dataset that has a column with 200k rows and every row presents different timestamps.
Example:
02/20/2019 01:30:00 PM
15/02/2019 13:30:00
I have tried to use in R Studio:
dataset <-as.POSIXlt(dataset,format= "%m/%d/%Y %H:%M")
dataset <-as.POSIXlt(dataset,format= "%H:%M:%S")
dataset <-as.POSIXlt(dataset,format= "%m/%d/%Y %I:%M:%S")
But the value changed for "01:30:00" what is considered "AM" or sometimes it brings "NA" as a result.
Do you know if there is another way?
See the parse_date_time() function from the lubridate package:
dat <- c("02/20/2019 01:30:00 PM", "15/02/2019 13:30:00")
lubridate::parse_date_time(dat, orders = c("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %I:%M:%S %p"))
[1] "2019-02-20 13:30:00 UTC" "2019-02-15 13:30:00 UTC"
Note that if you have other combinations, such month/day/year with the 24 hour clock time, you'll have to add that specification in the orders argument:
dat2 <- c("20/02/2019 01:30:00 PM", "15/02/2019 13:30:00")
lubridate::parse_date_time(dat2, orders = c("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %I:%M:%S %p")) # Wrong
[1] "2019-02-20 01:30:00 UTC" "2019-02-15 13:30:00 UTC"
lubridate::parse_date_time(dat2, orders = c("%d/%m/%Y %H:%M:%S", "%m/%d/%Y %I:%M:%S %p", "%d/%m/%Y %I:%M:%S %p")) # Right
[1] "2019-02-20 13:30:00 UTC" "2019-02-15 13:30:00 UTC"

Need to convert yyyy-mm-dd hh:mm:ss UTC to yyyy-mm-dd hh:mm format

How can I convert :
2019-01-22 18:30:33 UTC
to
2019-01-22 18:30
with delating ss and UTC word.
Thank you
Using base R:
Assuming your date is a string. Make it a POSIX object and format whichever way you want it.
date_as_posix <- strptime("2019-01-22 18:30:33 UTC", format="%Y-%m-%d %H:%M:%S", tz="UTC")
strftime(date_as_posix, format="%Y-%m-%d %H:%M", tz="UTC")
[1] "2019-01-22 18:30"
Best,
Chris
An option is floor_date assuming that the OP still want a Datetime object
library(lubridate)
floor_date(ymd_hms(str1), 'minute')
#[1] "2019-01-22 18:30:00 UTC"
If the intention is to get a string, then use format
format(ymd_hms(str1), "%Y-%m-%d %H:%M")
#[1] "2019-01-22 18:30"
data
str1 <- '2019-01-22 18:30:33 UTC'

Parsing datetime in AM/PM format

I am trying to parse Date-time in AM/PM format in R. I found that '%p' can handle this. However, when I try this:
mydate <- as.POSIXct("01.01.1970 01:00:00 PM", format="%d.%m.%Y %H:%M:%S %p", tz = "UTZ")
mydate
[1] "1970-01-01 01:00:00 UTZ"
> as.numeric(mydate)
[1] 3600
This is clearly 1 AM. I would have expected the output:
[1] "1970-01-01 13:00:00 UTZ"
[1] 46800
What am I missing?
It is considering as 1 AM instead of 1 PM, hence you get 3600 as output.
as.POSIXct("01.01.1970 01:00:00 PM", format="%d.%m.%Y %H:%M:%S %p", tz = "UTC")
#[1] "1970-01-01 01:00:00 UTC"
The document at ?strptime says
%p
AM/PM indicator in the locale. Used in conjunction with %I and not with %H. An empty string in some locales (and the behaviour is undefined if used for input in such a locale).
You need to use %I instead of %H
mydate <- as.POSIXct("01.01.1970 01:00:00 PM", format="%d.%m.%Y %I:%M:%S %p",
tz = "UTC")
as.numeric(mydate)
#[1] 46800
An alternative with lubridate
library(lubridate)
seconds(mdy_hms("01.01.1970 01:00:00 PM"))
#[1] "46800S"

How to convert "HH:MM:SS AM/PM" to 24 hour format in R?

Is there a way to convert an string in format "HH:MM:SS AM/PM" to 24 hour format in R.
I tried HMS but it didnot work.
When i am using strptime it is giving me date as well which i dont want.
strptime(c("1:00:29 AM","1:00:29 PM"), "%I:%M:%S %p")
#output "2016-08-07 01:00:29 IST" "2016-08-07 13:00:29 IST"
May be we can use format
time <- format(strptime(c("1:00:29 AM","1:00:29 PM"), "%I:%M:%S %p"), "%H:%M:%S")
time
#[1] "01:00:29" "13:00:29"
This can be converted to times class using ?times from chron
library(chron)
times(time)
#[1] 01:00:29 13:00:29

How do you convert time stamp that has AM/PM at the end of the string?

I am trying to convert this time stamp to POSIXct
t1 <- c("19-Jun-13 06.00.00.00 PM")
If I do this:
t1 <- as.POSIXct(t1, format="%d-%b-%y %H:%M:%S")
whould this convert this time stamp right? Does that considder the AM/PM at the end?
Read ?strptime. %p, which only works with %I, not %H. Your time format is also incorrect. Your times are separated by ".", not ":".
as.POSIXct("19-Jun-13 06.00.00.00 PM", format="%d-%b-%y %I.%M.%OS %p")
I don't understad why strptime doesn't recognize properly the %p format. However, the function dmy_hmsfrom package lubridate works well.
lubridate::dmy_hms("19-Jun-13 06.00.00.00 PM") creates the following result:
[1] "2013-06-19 18:00:00 UTC"
which you can "reformat" if you want, say Y-m-d H:M:
as.POSIXct(dmy_hms("19-Jun-13 06.00.00.00 PM"), format="%Y-%m-%d %H:%M")
[1] "2013-06-19 18:00:00 UTC"

Resources