Date-time Conversion in R with format and strptime

Each time I want to extract or show a specific attribute of a date or a time, I am lost in the documentation. What letter do I need to use in format() to get the name of the day written in full, in my language, from the current date? Is it “%Y %m %d”, “%a %b %d”, …?

So let’s keep it short this time!

Create a table of datetime conversion using format and specific locale

At the time I run this script, we are eaxctly:

today <- Sys.time()
today
## [1] "2020-09-10 22:53:48 CEST"

Now, I can use Sys.setlocale() to define the language in which I want to show the output. With all letters of the alphabet, upper and lowercase, I can now directly see what is the output of function format() applied on a date (i.e. 2020-09-10) or on a time (i.e. 2020-09-10 22:53:48).

lct <- Sys.getlocale("LC_TIME")

res <- tibble::tibble(
  format = paste0("%", sort(c(letters, LETTERS))),
  date_locale_C = {Sys.setlocale("LC_TIME", "C");format(Sys.Date(), format)},
  time_locale_C = {Sys.setlocale("LC_TIME", "C");format(today, format)},
  date_locale_fr = {Sys.setlocale("LC_TIME", "fr_FR.UTF-8");format(Sys.Date(), format)},
  time_locale_fr = {Sys.setlocale("LC_TIME", "fr_FR.UTF-8");format(today, format)},
  date_locale_en = {Sys.setlocale("LC_TIME", "en_GB.UTF-8");format(Sys.Date(), format)},
  time_locale_en = {Sys.setlocale("LC_TIME", "en_GB.UTF-8");format(today, format)}
)

Sys.setlocale("LC_TIME", lct)
## [1] "fr_FR.UTF-8"
knitr::kable(res)
format date_locale_C time_locale_C date_locale_fr time_locale_fr date_locale_en time_locale_en
%a Thu Thu jeu. jeu. Thu Thu
%A Thursday Thursday jeudi jeudi Thursday Thursday
%b Sep Sep sept. sept. Sep Sep
%B September September septembre septembre September September
%c Thu Sep 10 00:00:00 2020 Thu Sep 10 22:53:48 2020 jeu. 10 sept. 2020 00:00:00 jeu. 10 sept. 2020 22:53:48 Thu 10 Sep 2020 00:00:00 UTC Thu 10 Sep 2020 22:53:48 CEST
%C 20 20 20 20 20 20
%d 10 10 10 10 10 10
%D 09/10/20 09/10/20 09/10/20 09/10/20 09/10/20 09/10/20
%e 10 10 10 10 10 10
%E %E %E %E %E %E %E
%f %f %f %f %f %f %f
%F 2020-09-10 2020-09-10 2020-09-10 2020-09-10 2020-09-10 2020-09-10
%g 20 20 20 20 20 20
%G 2020 2020 2020 2020 2020 2020
%h Sep Sep sept. sept. Sep Sep
%H 00 22 00 22 00 22
%i %i %i %i %i %i %i
%I 12 10 12 10 12 10
%j 254 254 254 254 254 254
%J %J %J %J %J %J %J
%k 0 22 0 22 0 22
%K %K %K %K %K %K %K
%l 12 10 12 10 12 10
%L %L %L %L %L %L %L
%m 09 09 09 09 09 09
%M 00 53 00 53 00 53
%n
%N %N %N %N %N %N %N
%o %o %o %o %o %o %o
%O %O %O %O %O %O %O
%p AM PM am pm
%P am pm am pm
%q %q %q %q %q %q %q
%Q %Q %Q %Q %Q %Q %Q
%r 12:00:00 AM 10:53:48 PM 12:00:00 10:53:48 12:00:00 am UTC 10:53:48 pm CEST
%R 00:00 22:53 00:00 22:53 00:00 22:53
%s 1599696000 1599771228 1599696000 1599771228 1599696000 1599771228
%S 00 48 00 48 00 48
%t
%T 00:00:00 22:53:48 00:00:00 22:53:48 00:00:00 22:53:48
%u 4 4 4 4 4 4
%U 36 36 36 36 36 36
%v %v %v %v %v %v %v
%V 37 37 37 37 37 37
%w 4 4 4 4 4 4
%W 36 36 36 36 36 36
%x 09/10/20 09/10/20 10/09/2020 10/09/2020 10/09/20 10/09/20
%X 00:00:00 22:53:48 00:00:00 22:53:48 00:00:00 22:53:48
%y 20 20 20 20 20 20
%Y 2020 2020 2020 2020 2020 2020
%z +0000 +0200 +0000 +0200 +0000 +0200
%Z UTC CEST UTC CEST UTC CEST

Maybe this table of datetime conversion will be useful for other developers !



Citation:

For attribution, please cite this work as:
Rochette Sébastien. (2020, Sep. 10). "Date-time Conversion in R with format and strptime". Retrieved from https://statnmap.com/2020-09-10-date-time-conversion-in-r-with-format-and-strptime/.


BibTex citation:
@misc{Roche2020Date-,
    author = {Rochette Sébastien},
    title = {Date-time Conversion in R with format and strptime},
    url = {https://statnmap.com/2020-09-10-date-time-conversion-in-r-with-format-and-strptime/},
    year = {2020}
  }