# Calculating Time Periods in JS

I’m trying to put together a JavaScript function (and it has to be pure JavaScript, can’t use PHP or MySQL) to work out how many weeks between two dates. We have a twelve week roster and I want to work out where in the roster each person will be on a given date (and week).

I have a function to create a date object for each:

//Converts a string into a date
function calcDate(strDate) {

//Trim any extra spaces
strDate = strDate.trim( )

//Change all the delimiters to a stroke
strDate = strDate.replace(/ /g,"/");
strDate = strDate.replace(/\\ /g,"/");
strDate = strDate.replace(/\-/g,"/");
strDate = strDate.replace(/\./g,"/");

var intYear = strDate.slice(strDate.lastIndexOf("/") + 1);
var intMonth = strDate.slice(strDate.indexOf("/")+1,strDate.lastIndexOf("/"));
var intDay = strDate.slice(0, strDate.indexOf("/"));

return new Date(intYear,intMonth-1,intDay);

}

I then use the code below to work out how many weeks there are between two dates:

//Create a variable for a day in milliseconds
var dayLength = 1000*60*60*24;

//Get the date input by the user and the roster start date
var chosenDate = calcDate(document.getElementById("txtDate").value);
var startDate = calcDate(document.getElementById("txtStartDate").value);

//Work out the difference in weeks
var intWeeks = Math.floor(((chosenDate-startDate)/dayLength)/7);

//Divide by link size and get the remainder to simplify the maths

//Display the new Date
document.getElementById("newDate").innerHTML = emptyString.concat("<p>The date you have selected is: ", chosenDate, ", ", intWeeks, " weeks into the cycle.</p>");

This works fine, except of course when BST comes into play - on the first day of BST, although the last bit (display the date) shows the right date, it is doing it’s calculations based on GMT, so it thinks it’s 23:00 the previous day and therefore it thinks we are a week behind in the roster cycle.

Have I missed something stupid?

Thanks
Chris

No, nothing stupid, it’s just a complex problem, as it nicely explained by Tom Scott.

1 Like

The first day of BST is the last Sunday or March, that being Sunday the 29th of March 2020.

I’m having trouble finding any trouble there.

Can you please give details of the txtDate value, txtStartDate value, linkSize value that you are having trouble with?

You should Math.round instead of Math.floor. When the start date and the chosen date are in the same time period “(chosenDate-startDate)/dayLength” returns the exact number of days. When they are not, they either return “days - (1/24)” or “days + (1/24)”.

Hi, I got your point. You can check this JS solution to find week in date interval.

function weekFinder(d1, d2) {
return Math.round((d2 - d1) / (7 * 24 * 60 * 60 * 1000));
}

For details view you can visit the resource as well. Stack Overflow.

Thanks.
Jack

fake signature removed by gandalf458

Date.parse(document.getElementById("txtDate").value) - Date.parse(document.getElementById("txtStartDate").value) / 604800000

(and if you really want to floor/round, do so.)

Thanks for you all your help - that’s a great little video! I’ve got it working now, I hadn’t thought of using Math.round.

One other question - the reason I’m doing all this in JavaScript is to keep it all in a self-contained file that can be stored and viewed locally without requiring internet. However, friends with iPhones tell me that the file loads but the the JavaScript doesn’t work.

Any ideas why? The best I can see is to use a third-party app to open it, but I can’t find a way of either opening the file from FireFox, or forcing it to open in FireFox rather than the default app.

Thanks
Chris

Have you opened the site on an iPhone? What browser are they using?

It’s probably that the browser they’re using doesnt support one of the commands you’re using, but the first step is to find out what browser it is.

Don’t know if you’ve ever messed around with moment.js, but it might be worth a look-see.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.