Dylan
Dylan

Reputation: 317

How do I calculate week difference in Javascript from the HTML week form?

I'm looking for a way to fill a week form element according to the input of another week form element. I'm pretty new to Javascript.

Basically I need to get the difference of week between c1 and c2 form. Then fill d1 and d2 automatically with the same difference of week but with d1 starting one week after c2.

Exemple:

c1 = "2018-W01"

c2 = "2018-W05"

Should set d1 and d2 values to:

d1 = "2018-W06"

d2 = "2018-W10"

    <input type="week" id="c1">
    <input type="week" id="c2">
    <input type="week" id="d1" readonly>
    <input type="week" id="d2" readonly>

<script>
function myFunction() {
    var d1 = document.getElementById("c1").value;
    var d2 = document.getElementById("c2").value;
    var year = d1.substring(0,4); //2018
    var day1 = d1.substring(6,);  //01
    var day2 = d2.substring(6,);  //05

    var difference = day2 - day1; //5

    document.getElementById("d1").value = d1 + difference; // ?
    document.getElementById("d2").value = d2 + difference;
</script> 

This piece of code obviously doesn't work but shows what I think. This does rise a problem with dates that overlap between two years. Thanks for the help!

Upvotes: 0

Views: 200

Answers (6)

Muhammad Saqib
Muhammad Saqib

Reputation: 70

try this..

function myFunction(d1, d2) {

var year1 = parseInt(d1.substring(0, 4)); //2018
var year2 = parseInt(d2.substring(0, 4));
var yearDiff = Math.abs(year1 - year2);
var weeksInYears = 52 * yearDiff;

var day1 = parseInt(d1.substring(6, ));  //01
var day2 = parseInt(d2.substring(6, ));  //05

var difference = Math.abs(weeksInYears - (day2 - day1));  // handled dates with different years
var week1 = day2 + 1;
var week2 = week1 + difference;
week1 = ((week1 < 10 ? '0' : '') + week1);
week2 = ((week2 < 10 ? '0' : '') + week2)
var res = year1 + "-" + "W" + (week1); // ?
var res1 = year2 + "-" + "W" + (week2);
console.log(res, res1);}

Upvotes: 1

David Castro
David Castro

Reputation: 1957

momentjs is the library you should use to manage date and time.

it will help you to manage each value of a date in a simple way.

var a = moment('2016-01-01'); 
var b = a.add(1, 'week'); 
a.format();
"2016-01-08T00:00:00-06:00"

See this examples:

https://codepen.io/bassfiddle/pen/lrLwy

Upvotes: 0

Lucifer
Lucifer

Reputation: 1

Hope, i could help you... I had use this in one of my project.

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,4);
dt2 = new Date(2014,10,12);
console.log(diff_weeks(dt1, dt2));

dt1 = new Date("June 4, 1990 08:11:00");
dt2 = new Date("October 10, 2018 11:13:00");
console.log(diff_weeks(dt1, dt2));

Upvotes: 0

sridhar..
sridhar..

Reputation: 2133

Try this out.

I want to get complete years, (that contains 52 weeks ), so i used

 year2-(year1+1),  

So as to exclude both years, then i am adding year1 weeks (52-week1)

and then added week2

    var year1 = d1.substring(0,4); //2017
    var year2 = d1.substring(0,4); //2018
    var week1 = d1.substring(6,);  //01
    var week2 = d2.substring(6,);  //05
    let result = 52 *( year2 - (year1 + 1) ) + (52 - week1) + week2

Upvotes: 0

Norair
Norair

Reputation: 74

Change these two lines

   var day1 = parseInt(d1.substring(6,));  //1
    var day2 = parseInt(d2.substring(6,));  //5

Then it should work but its better to work with Javascript Dates -> Good reference https://www.w3schools.com/js/js_dates.asp

Upvotes: 0

Jarek Wichrowski
Jarek Wichrowski

Reputation: 256

In JS can use https://momentjs.com/

This lib resolve your problem with any operations on the date

Upvotes: 0

Related Questions