CodexBloom - Programming Q&A Platform

implementing date-time conversion in R when using `lubridate::ymd_hms` with locale settings

👀 Views: 437 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
r lubridate datetime R

I'm trying to implement Can someone help me understand I'm attempting to set up Hey everyone, I'm running into an issue that's driving me crazy... I tried several approaches but none seem to work. I'm working with an scenario when trying to convert date-time strings into POSIXct objects using `lubridate::ymd_hms` while working in a non-default locale. My date-time strings are in the format "YYYY-MM-DD HH:MM:SS", but I'm getting unexpected results that do not match the input data. For instance, when I run the following code: ```r library(lubridate) date_strings <- c("2023-03-15 14:30:00", "2023-04-16 10:00:00") converted_dates <- ymd_hms(date_strings) print(converted_dates) ``` I expect to see a vector of POSIXct date-times, but I am getting this warning: ``` Warning message: In ymd_hms(date_strings) : NAs introduced by coercion ``` I suspect that the locale settings might be affecting the parsing, especially since I'm working in a locale where the default date format is different. I have tried explicitly setting the locale with `Sys.setlocale("LC_TIME", "C")`, but it didn't resolve the scenario. Could someone guide to figure out how to properly convert these date-time strings without running into NA values? I'm using R version 4.3.1 and `lubridate` version 1.8.0. Has anyone else encountered this? Am I approaching this the right way? I'm on CentOS using the latest version of R. Thanks for any help you can provide! This issue appeared after updating to R latest. Is there a better approach?