Messy affair of DATEing
Dates are always a messy affair
My apologies if you thought I would be talking about dates, yes definitely I will be talking about dates but not that type of date you thinking but the actual dates.
Do you love dates, personally I do but they can be a mouthful of headaches if you understand what I mean. So here goes my dating scenes.
Planning for dates
You cannot talk about date without specifying time. They are kind of intertwined somehow. In fact there is even an ISO Standard 1860 specifically for this here, we should all follow this when dating and timing. Actual format YYYY-MM-DD HH:MM:SS or 2023-10-15 00:19:43.825418.
When planning dates the details are really important.
Preparing for dates
I would advocate using lubridate
r package for easier understanding and manipulation of dates rather. I find it easier than base R but to each their own. Your date is your date and we cannot be all on the same date.
Functions within dates
Function | Description |
---|---|
now() |
Returns date and time right now, at this instance |
today() |
Returns the date today |
parse_date_time |
Returns date from list of text you can specify the ordering e.g. ymd, dmy, mdy where y (year), m (month) and d (day) |
Manipulating your date
You can always apply mathematical parameters when dealing with dates mainly +, -
. Adding and subtracting days to come up with future or past dates.
With this we need to understand two principles:
period
:- Human time value e.g. months, days, years, hours. R respective codesdays
,hours
,dweeks
.duration
:- Time value based in seconds calculating like a stop watch time for 86400 secs. Doesn’t adhere to leap years, day light saving and all. R respective codesddays
,dhours
,dweeks
addition of prefixd
.
When adhering to date rules i.e leap years we can use %m+%
to add days and %m-%
for subtracting.
library(lubridate)
first_corona_case_date = parse_date_time("12 January 2020", orders = "dmy" ,tz = "")
first_corona_case_date
## [1] "2020-01-12 EAT"
The first case of corona (COVID-19) was reported on 2020-01-12 and 3 months down we first the a million case.
million_corona_case_date = first_corona_case_date + months(3)
million_corona_case_date
## [1] "2020-04-12 EAT"
The first million case reported was 2020-04-12
How do we sort long distance dating
You planned for the date but your partner is in a different timezone how do you align the time. Now the particulars of your date apart from time has an extra component of timezone.
Assuming you are based in Tokyo and your partner is abased in Nariobi . First when using R we need to pick the list of city time zone OlsonNames
e.g. Africa/Abidjan for Tokyo name is Africa/Nairobi and Nairobi is Asia/Tokyo
When Nairobi partner says the call is happening 8PM the parner in Tokyo has to use the with_tz()
function in R to calculate the time.
Nariobi_time <- force_tz(as_datetime("2023-09-01 20:00:00"), tz = "Africa/Nairobi")
Nariobi_time
## [1] "2023-09-01 20:00:00 EAT"
Tokyo_time <- with_tz(Nariobi_time, tzone = "Asia/Tokyo")
Tokyo_time
## [1] "2023-09-02 02:00:00 JST"