Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R lubridate package date-time creation omits time at midnight

I am trying to create date-time using lubridate package, and I've tried the following values:

library(lubridate)
ymd_hms("2017-07-02 23:00:00")
[1] "2017-07-02 23:00:00 UTC"
ymd_hms("2017-07-02 00:00:00")
[1] "2017-07-02 UTC"

It seems that if you type the time at midnight, 00:00:00, the ymd_hms() method ignores the time value, and the result becomes a date. Is there any way to show the 00:00:00 in the result? Thanks a lot!

like image 681
ACuriousCat Avatar asked Jul 05 '18 15:07

ACuriousCat


People also ask

What does the Lubridate package do?

lubridate: Make Dealing with Dates a Little EasierFunctions to work with date-times and time-spans: fast and user friendly parsing of date-time data, extraction and updating of components of a date-time (years, months, days, hours, minutes, and seconds), algebraic manipulation on date-time and time-span objects.

What does the Lubridate do in R?

Lubridate makes it easier to do the things R does with date-times and possible to do the things R does not. If you are new to lubridate, the best place to start is the date and times chapter in R for data science.

Is Lubridate included in Tidyverse?

lubridate is part of Hadley's tidyverse ecosystem but is not loaded by the tidyverse package, which includes only what he thought were the core components.


1 Answers

So I just got my own answer to this question after google searches and error trying:

mydates <- format(as.POSIXct("2011-01-01 00:00:00", tz = "UTC"), "%m-%d-%Y %H:%M:%S")
mydates
[1] "01-01-2011 00:00:00"
format(as.POSIXct("2011-01-01 00:00:00", tz = "UTC"), "%m-%d-%Y %H:%M:%S")
[1] "01-01-2011 00:00:00"

It seems that using the R Base as.POSIXct with format arguments works.

like image 91
ACuriousCat Avatar answered Sep 28 '22 01:09

ACuriousCat