mbrapp
mbrapp

Reputation: 1

Converting from military time to standard time, indicating AM/PM, using Lubridate package

I am trying to convert a column that's part of a data frame from military time to standard time. I would also like to indicate AM/PM. I'm trying to do this within the capabilities of the R's lubridate package.

from another data frame called "df" I've created a new data frame called "out" with the following code:

out <- df %>% mutate(NewDate = parse_date_time(CreatedDate, orders = "mdy IMS p")) %>%
  select(CreatedDate, NewDate) 

The "df" column I'm manipulating with the mutate function, called "CreatedDate", has cells that look like this:

01/01/2019 02:12:37 PM
01/01/2019 10:37:57 PM
01/02/2014 05:10:35 AM

I have manipulated that column to produce the new column called "NewDate" which looks like this:

2018-04-12 11:11:26
2018-11-13 13:06:56
2018-04-16 05:30:13

I'm trying to get the column "NewDate" to be standardized but on regular time and to include the AM/PM designation, like it did before I applied the parse_date_time function.

I expected that, by including the "mdy IMS p" within my parse_date_time function, I would have generated content within my NewDate column that looks like this:

2018-04-12 11:11:26 AM
2018-11-13 01:06:56 PM
2018-04-16 05:30:13 AM

Upvotes: 0

Views: 1893

Answers (2)

djhocking
djhocking

Reputation: 1112

If you want to use lubridate for consistency and for use with other aspects of code, you can use the same code as @Jav posted in the orders argument. This can be helpful if you have dates that are inconsistently formatted. If some were in military and some in AM/PM format you could do something like:

df <- data.frame(CreatedDate = c("01/01/2019 02:12:37 PM",
                                 "01/11/2019 06:12:37 PM",
                                 "01/01/19 10:37:57 PM",
                                  "2019-01-02 21:17:12"),
                 stringsAsFactors = FALSE)

You and see all the mixed up formatting, which is unfortunately uncommon.

out <- df %>%
  mutate(NewDate = parse_date_time(CreatedDate, 
                                   orders = c("%m/%d/%Y %I:%M:%S %p", 
                                              "%m/%d/%y %I:%M:%S %p",
                                              "%Y-%m-%d %H:%M:%S"))) %>%
           dplyr::select(CreatedDate, NewDate)

The end results should be new dates with consistent formatting as you can see below:

             CreatedDate             NewDate
1 01/01/2019 02:12:37 PM 2019-01-01 14:12:37
2 01/11/2019 06:12:37 PM 2019-01-11 18:12:37
3   01/01/19 10:37:57 PM 2019-01-01 22:37:57
4    2019-01-02 21:17:12 2019-01-02 21:17:12

Upvotes: 2

Jav
Jav

Reputation: 2313

You can do it in base R with format

date_time <- as.POSIXct("2018-04-12 21:11:26")
format(date_time, "%Y-%m-%d %I:%M:%S %p")

# [1] "2018-04-12 09:11:26 PM"

%Y - 4 digit year

%m- decimal month

%d - decimal date

%H - decimal hour (24 hours)

%I - decimal hour (12 hours)

%M - decimal minute

%S - decimal second

%p - AM/PM

Upvotes: 5

Related Questions