1

I have a very specific problem. I have been trying to convert a date time character into a date time format in R. Example: "2017-05-21 00:00:00". Whenever I try to convert it using strptime and as.POSIXct to a date time format it gives me "2017-05-21".

Thanks for any help

Fiona
  • 477
  • 1
  • 9
  • 18
  • Seems to work fine for me. Try: `as.POSIXct("2017-05-21 00:00:00",format='%Y-%m-%d %H:%M:%S')+1` to see that while it only shows you `"2017-05-21 CEST"`, the time part is included but just omitted from the output. – Florian Jan 31 '18 at 13:49
  • 1
    That datetime is 1495339200 seconds after 1970-01-01 and what you see printed is just a matter of human-readable formatting. Sort of like if you enter `(x <- 1.000000000)` the output is just `1`. – ngm Jan 31 '18 at 13:56

1 Answers1

4

As @ngm says, this is only a formatting choice on the part of R. You can check to make sure it's actually midnight. Datetimes are stored as seconds past the epoch, and can actually be used in arithmetic.

t1 <- as.POSIXct("2017-05-21 00:00:00")
t1
# [1] "2017-05-21 EDT"

as.integer(t1)
# [1] 1495339200

So your time is 1,495,339,200 seconds after the epoch. Now we can look at midnight plus one second.

t2 <- as.POSIXct("2017-05-21 00:00:01")
t2
# [1] "2017-05-21 00:00:01 EDT"

as.integer(t2)
# [1] 1495339201

Which is one second higher than t1. So t1 is, in fact, midnight.

Nathan Werth
  • 5,093
  • 18
  • 25
  • So R should realise when it converts "2017-05-21 00:00:00" to a date time format that if it shows "2017-05-21" it's at midnight on that day? – Fiona Jan 31 '18 at 14:09
  • R doesn't show all the zeros because it doesn't think they'd be helpful, so it's up to us to know it means midnight. Again, like @ngm pointed out, it "strips" the trailing time portion when it's all zeros (i.e., midnight). With `t1` from my example, if you print `c(t1, t2)`, it will print the zeros for the first value. The difference is in formatting, not the data. – Nathan Werth Jan 31 '18 at 14:34
  • But if I need to compare two date times surely not having the zeros there will impact the result? – Fiona Jan 31 '18 at 15:01
  • 1
    No, it won't. The underlying data is not what's being shown when printed. `print` just gives us a nice-looking blurb of text meant for humans to understand. Try `identical(t1, as.POSIXct("2017-05-21 00:00:00"))`, and you'll see they're the same. – Nathan Werth Jan 31 '18 at 17:18