1

So, I have a cal-heatmap calendar working on my website. It displays data from December 2016 to November 2017.

On the same page, I have texts which are divided into different sections.

Here is the code for a section:

<h3 class="sectiontitle" id="week1" style="clear:both;">Week 1</h3>
<div class="divider"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>

There is one section for every week in December 2016 to November 2017.

What I want is onClick on cal-heatmap's day, it will scroll to the corresponding section.

For example,

  • onclick 3rd December 2016, it will link to week 1.
  • onclick 4th December 2016, it will link to week 1.
  • onclick 10th December 2016, it will link to week 2.
  • onclick 28th December 2016, it will link to week 5.

According to cal-heatmap's documentation, I should use onClick(date, value) function. However, I don't know how to change the date given into the week number.

Please note that the date given is in this format: "Tue Jan 04 2000 17:00:00 GMT+0800 (China Standard Time)", not in timestamp format.

So, how can I achieve this? Thanks! I hope that I have explained it clearly.

gabrielchl
  • 606
  • 9
  • 20
  • 2
    If 1st of December is Thursday, does it count as 1st week. Or Rather your 1st week is from 5th of December which is Monday? – iMatoria Aug 14 '17 at 03:47
  • @iMatoria it's also fine to have 5th of December as the start of 1st week. – gabrielchl Aug 14 '17 at 03:55
  • I just don't know how to get the week number from the given date – gabrielchl Aug 14 '17 at 03:55
  • 2
    You need to write a helper function which will accept a date and you need to first get day for that date. And according to day you should add number of days to that date to make it as Monday. Now divide it by 7 to get the week number. – iMatoria Aug 14 '17 at 04:04
  • @iMatoria I see. Is it possible to change "Tue Jan 04 2000 17:00:00 GMT+0800 (China Standard Time)" into timestamp? – gabrielchl Aug 14 '17 at 04:05

1 Answers1

1

Thanks to @iMatoria , I was able to find the answer.

var week = Math.ceil(((date.getTime() / 1000 - 1480435200) / 86400 - 4) / 7 + 1);

This will give the week number since first day. Where '1480435200' is 31st of November, 2016

Working example: http://2017.igem.org/Team:Hong_Kong_UCCKE/Notebook

gabrielchl
  • 606
  • 9
  • 20