convert lubridate::date to a formated date

Hi,
I am new to lubridate
I am taking the function

library (lubridate)
	date1<-lubridate:: date ()
date1
[1] "Sun Mar 17 20:17:39 2024"
class (date1)
[1] "character"

I would like to format this "Sun Mar 17 20:17:39 2024" into "Sunday 17-03-2024; 20:20"
I cannot manage it by using ymd_hm () or any other lubridate function
Thanks

1 Like

You can change the string "Sun Mar 17 20:17:39 2024" into the string "Sunday 17-03-2024; 20:20" in a few steps. The only use of lubridate in my code is to round the value to the nearest 5 minutes. The rounding is my guess at your intention in changing the time from 20:17:39 to 20:20.
Since the final result is a string, you can't do calculations or most plotting functions with it.

Timestamp <-  as.POSIXct("Sun Mar 17 20:17:39 2024", format="%a %b %d %H:%M:%S %Y")
Timestamp 
#> [1] "2024-03-17 20:17:39 MDT"
RndTimestamp <- lubridate::round_date(Timestamp, unit = "5 minutes")
RndTimestamp
#> [1] "2024-03-17 20:20:00 MDT"
strftime(RndTimestamp, format = "%A %d-%m-%Y; %H:%M")
#> [1] "Sunday 17-03-2024; 20:20"

Created on 2024-03-17 with reprex v2.0.2

1 Like

Hi,
Many thanks for your time and help,
However, when I use this code in RStudio, I get this

Timestamp <- as.POSIXct("Sun Mar 17 20:17:39 2024", format="%a %b %d %H:%M:%S %Y")
Timestamp 
[1] NA

Thanks

Very strange. I copied your text and it ran with no problem.

I wonder if you have some package loaded that is generating a conflict? What happens if you try the code in a new session?

The help for as.POSIXct() includes the following

If format is specified, remember that some of the format specifications are locale-specific, and you may need to set the LC_TIME category appropriately via Sys.setlocale . This most often affects the use of %a , %A (weekday names), %b , %B (month names) and %p (AM/PM).

I have never used Sys.setlocale, so I can't offer guidance immediately but I'll look into what needs to be done.

The correct setting for the LC_TIME category is specific to the operating system. On my Windows 10 system in the US, I get

> Sys.getlocale()
[1] "LC_COLLATE=English_United States.utf8;LC_CTYPE=English_United States.utf8;LC_MONETARY=English_United States.utf8;LC_NUMERIC=C;LC_TIME=English_United States.utf8"

What operating system do you have?

You are right. This is what I get
I have changed the Sys.setlocale("LC_TIME", "English")

Sys.getlocale()
#> [1] "LC_COLLATE=Spanish_Spain.utf8;LC_CTYPE=Spanish_Spain.utf8;LC_MONETARY=Spanish_Spain.utf8;LC_NUMERIC=C;LC_TIME=Spanish_Spain.utf8"
Sys.setlocale("LC_TIME", "English")
#> [1] "English_United States.1252"
Sys.getlocale()
#> [1] "LC_COLLATE=Spanish_Spain.utf8;LC_CTYPE=Spanish_Spain.utf8;LC_MONETARY=Spanish_Spain.utf8;LC_NUMERIC=C;LC_TIME=English_United States.1252"

I will try your code again
Created on 2024-03-18 with [reprex v2.1.0]

I am running Windows 11. I got it by changing only
I have changed the Sys.setlocale("LC_TIME", "English")
This is what I get now:

library (lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
    time1<- lubridate::date ()
    time2<-as.POSIXct (time1, format="%a %b %d %H:%M:%S %Y")
    timestamp <- lubridate::round_date(time2, unit = "5 minutes")
    strftime(timestamp, format = "%A %d-%m-%Y; %H:%M")
#> [1] "Monday 18-03-2024; 16:50"

Many thanks!

I think the easiest way to achieve your desired result is lubridate::timestamp(), check it out.

 library (lubridate)
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
timestamp()
##------ Fri Mar 22 14:04:22 2024 ------##

Excellent alternative, thank you!

1 Like

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.