Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleCalendar Datekey

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?

like image 857
jantimon Avatar asked Aug 28 '26 16:08

jantimon


1 Answers

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:

0 years (since 1970) * 512
+ 1 month * 32
+ 1 day
= 33

To calculate the datekey for 01.01.2000 you would calculate:

30 years (since 1970) * 512
+ 1 month * 32
+ 1 day
= 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?

like image 82
jantimon Avatar answered Aug 31 '26 05:08

jantimon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!