Date to POSIXct conversion

When I try to convert a date with a year, month and day information of class Date to POSIXct, I get a result consisting also of hours, minutes,etc but the hour is set to "01". I think this is related to the locale which is set to Europe (LC_TIME=fr_FR.UTF-8) and the default timezone is CET.

The example can be duplicated using the chunk of code below:
Sys.Date() # "2019-01-24" : This gives back a Date class object
as.POSIXct(Sys.Date()) # "2019-01-24 01:00:00 CET": This gives back a POSIXct

I would like to get a "00" hour when converting to POSIXct and to understand what's really going. Is there a way to accomplish this without using lubridate?


You are right; this is linked to your locale being the "Europe/Paris" timezone.

When you as.POSIXct() a date, it:

  1. Coerces the date to a datetime, using the start of the date in the UTC context.
  2. Changes the context of the datetime to your local context, e.g. "Europe/Paris".

There are ways around this, made easier using the "lubridate" package, but to solve the problem you will need to specify the timezone context for which you will want the "00-hour" result.

Hope this helps!

Thanks but here is where I get stuck for two reasons:
1- By default, my locale shoud be the default value and so the "tz" argument should be the right one for this usage.
2- Even when I specify tz="Europe/Paris", the result has hours set to "01"

It might be useful to convert to character, then parse:


ymd(as.character(Sys.Date()), tz = "Europe/Paris")

That is exactly what I did to solve it. Yet, I'm still a bit curious on how to get around it using base R... Just a matter of curiosity. So, I'll leave the post open for now...
Thanks a lot for your help!

1 Like

In this case, I think you can just use as.POSIXct() in place of ymd(). Glad it's sorted!

1 Like

Thanks a lot ! It's that simple after all. When applying POSIXct to a character, it just throws away the less than day unit information!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.