Google is using an unique DateKeys for each days in the GoogleCalendar HTML e.g.
<div
data-datekey="129"
role="gridcell"
tabindex="-1"
jsname="RjPD4e"
aria-labelledby="tsc-0"
data-column-index="0"
data-principal-ids="amFuLm5pY2tsYXNAbmFtaWNzLmNvbQ"
class="YvjgZe Qbfsob">
Is there any formular to calculate the date for a given datekey?
It looks like the dateKey represents the days since 1.1.1970 in a format optimised for byte shifting.
One year has 2^9 (512) days.
One month has 2^5 (32) days.
To calculate the datekey for 01.01.1970 you would calculate:
0years (since 1970)* 512
+ 1month* 32
+ 1day
= 33
To calculate the datekey for 01.01.2000 you would calculate:
30years (since 1970)* 512
+ 1month* 32
+ 1day
= 15393
To calculate the date for a given date key you can do the opposite. A modulo calculation could look like the following:
function getDate(dateKey) {
const yearOffset = (dateKey - 32) % 512;
const year = (dateKey - 32 - yearOffset) / 512;
const day = yearOffset % 32;
const month = (yearOffset - day) / 32;
return new Date(year + 1970, month, day);
}
Does anyone know why they came up with such a logic?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With