38

How shall I convert this to a datetime type object which preserves both the date and the time?

DateTime="2007-02-01 00:00:00"

Tried

as.Date(DateTime,'%Y-%m-%d %H:%M:%S') 

but doesn't return the time part. I couldn't figure out how after trying strptime and lubridate.

MendelG
  • 14,885
  • 4
  • 25
  • 52
santoku
  • 3,297
  • 7
  • 48
  • 76

2 Answers2

55

As @Richard Scriven pointed out, you shouldn't be using as.Date because it's not a datetime class. Here are a few different ways:

DateTime <- "2007-02-01 00:00:00"
DateTime2 <- "02/01/2007 00:06:10"
## default format Y-m-d H:M:S
> as.POSIXct(DateTime,tz=Sys.timezone())
[1] "2007-02-01 EST"
> as.POSIXlt(DateTime,tz=Sys.timezone())
[1] "2007-02-01 EST"
##
## specify format m/d/Y H:M:S
> as.POSIXct(DateTime2,format="%m/%d/%Y %H:%M:%S",tz=Sys.timezone())
[1] "2007-02-01 00:06:10 EST"
> as.POSIXlt(DateTime2,format="%m/%d/%Y %H:%M:%S",tz=Sys.timezone())
[1] "2007-02-01 00:06:10 EST"
##
## using lubridate
library(lubridate)
> ymd_hms(DateTime,tz=Sys.timezone())
[1] "2007-02-01 EST"
> mdy_hms(DateTime2,tz=Sys.timezone())
[1] "2007-02-01 00:06:10 EST"

You don't have to specify format= for as.POSIXct and as.POSIXlt when you have the %Y-%m-%d %H:%M:%S format. In other cases, like %m/%d/%Y %H:%M:%S, you usually have to specify the format explicitly.

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • 3
    I could be wrong. But, it seems to me that the OP wants to keep both date and time as they are in the string. `2007-02-01 EST` may not be the one the OP wants since it does not contain `00:00:00` in the outcome. – jazzurro Sep 21 '14 at 15:32
  • Hmm that's a fair point. I know that if you have a datetime with `00:00:00` and another datetime that has nonzero HMS in a `data.frame`, etc... it prints the zeros - i.e `data.frame(x=c(as.POSIXct(DateTime,tz=Sys.timezone()),as.POSIXct(DateTime,tz=Sys.timezone())+1))` (using the objects above); I'll see if there's a way to do this with individual timestamps though. – nrussell Sep 21 '14 at 15:44
  • Thanks! seems in DateTime format the hms is not showing, but in DateTime2 format it is. – santoku Sep 21 '14 at 16:01
  • 1
    Right - this only happens with midnight (`00:00:00`) - and it still retains the information (it just won't print it). It shows the zeros if you view such a timestamp with other non-midnight times, like in my comment above, otherwise you have to kind of hack the formatting to get around this behavior, like in @jazzurro's solution. – nrussell Sep 21 '14 at 16:04
  • @nrussell I thought this case was addressing this special `00:00:00` case. But it seems that I investigated the question a bit more than necessary. – jazzurro Sep 21 '14 at 16:06
  • @jazzurro That's not a bad thing - there doesn't seem to be a straightforward way to print midnight times, so your solution is definitely worth contributing. – nrussell Sep 21 '14 at 16:10
  • @nrussell I wonder if this is a question worth to ask. I actually did not notice this case till now. – jazzurro Sep 21 '14 at 16:12
  • @jazzurro I did a brief Google search and didn't come across a good solution. Some people seem to think it's a bug, but as far as I can tell this is done intentionally. You might consider posting it as a question and adding your solution below as an answer, just so it's more convenient to find for someone who has this problem in the future. – nrussell Sep 21 '14 at 16:18
  • @nrussell Thanks for your support. What is the intention for this special `00:00:00` behaviour? What benefit do we have? – jazzurro Sep 21 '14 at 16:26
  • 1
    Unless you can find it in the documentation, I think that would be a very good question. For example, with `lubridate` loaded, try `dt <- as.POSIXct("2007-02-01 00:00:00"); hour(dt) <- hour(dt)+1; dt; hour(dt) <- hour(dt)-1; dt`. Midnight seems to never appear – Rich Scriven Sep 21 '14 at 17:02
  • @RichardScriven Thank you for the codes. I tested them. 00:00:00 did not appear. I will post a question with my answer. – jazzurro Sep 21 '14 at 21:59
  • Where does the `timezone` function come from? – Julien Aug 15 '22 at 07:34
4

If you want to specifically convert "2007-02-01 00:00:00" to a date-class object, this is what you need to do. This is based on this question and answer

print.POSIXct <- function(x,...)print(format(x,"%Y-%m-%d %H:%M:%S"))
x <- "2007-02-01 00:00:00"
x <- as.POSIXct(x,tz=Sys.timezone())
x
Community
  • 1
  • 1
jazzurro
  • 23,179
  • 35
  • 66
  • 76