Date, year, week conversion

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

We create the following functions to make it easier to work with date and times in different formats.

For the input, we allow either a numeric (3) or string ("03", "2020-03").

For the output, the user need to specify whether numeric or string is required. Functions end with c returns a character output and n returns a double.

library(cstime)
library(magrittr)

Which year/week is this date?

By default the functions returns the isoyear or isoweek of today.

date_to_isoyear_c()
date_to_isoyear_n()
date_to_isoweek_c()
date_to_isoweek_n()
# provide a date
date_to_isoyear_c('2021-01-01')
date_to_isoyear_n('2021-01-01')
date_to_isoweek_c('2021-01-01')
date_to_isoweek_n('2021-01-01')
date_to_isoyearweek_c('2021-08-11')

Get isoyear/isoweek from a isoyearweek string

isoyearweek_to_isoyear_c("2021-02")
isoyearweek_to_isoyear_n("2021-02")
isoyearweek_to_isoweek_c("2021-02")
isoyearweek_to_isoweek_n("2021-02")

A list of isoyearweek can be accessed in the following way.

yrwk_19_20 <- dates_by_isoyearweek[isoyear %in% c(2019, 2020)]
head(yrwk_19_20)


Try the cstime package in your browser

Any scripts or data that you put into this service are public.

cstime documentation built on May 31, 2023, 9:27 p.m.