How to calculate the number of the week from the start and end date period?


the main idea is to get the number of the week between two dates (from a period of start date and end date)!
Something like that: if the period is 01-05-2020 to 31-05-2020 and in the data picker I chouse 08-05-2020 result will be 2, the second week.

can someone help with that can’t figure out by my self,
thank you!

here is JS date picker code line with setting up period 01-05-2020 to 31-05-2020, how to echo out from this js code number of week for future php usage or input value?


<!-- js -->
<script src='' type='text/javascript'></script>
<!-- Datepicker -->
<link href='' rel='stylesheet' type='text/css'>
<script src='' type='text/javascript'></script>

<script type="text/javascript">

    format: "dd/mm/yyyy",
    startDate: new Date('2020-5-1'),
    endDate: new Date('2020-5-31')



<div class="form-group">
<label for="formGroupExampleInput">Date of sale</label>
<input type="text" name="dos" value="<?php echo $doc; ?>" class="form-control" id='datepicker' id="formGroupExampleInput">


Thanks, I found a solution to my problem.
Here it is, maybe someone needed..

function diff_weeks(dt2, dt1) 

  var diff =(dt2.getTime() - dt1.getTime()) / 1000;
  diff /= (60 * 60 * 24 * 7);
  return Math.abs(Math.round(diff));


dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,11);
console.log(diff_weeks(dt1, dt2));

dt1 = new Date("June 13, 2014 08:11:00");
dt2 = new Date("October 19, 2014 11:13:00");
console.log(diff_weeks(dt1, dt2));


Answered By – Axīc

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published