1

Is there any coding (php / HTML or JS script) to display a week counter and start this counter from a specific date say i wanted to start this counter on Monday 25th March 2019 (and just display "Week 1"), then the number increases every week or 7 days (so on Monday 1st April 2019 it would change to "Week 2") until it gets to 52 weeks? Any help would be grateful!!!! thanks!

1 Answer 1

1

This can be done with plain JavaScript using the Date object.

First initialize a new Date from which you want to start counting

var startDate=new Date(2019,2,10); // will set the date to the 10th of March

Second get the actual time and date

var today=new Date();

Now simply get the difference between this two dates like this:

var difference=today-startDate;

This will return the difference in milliseconds.

To get the difference in days proceed like this

var days = difference / 1000 / 60 / 60 / 24;

Based on this you can get the number of weeks that passed by

var weeks = parseInt(days / 7);

Here's a working example:

var startDate = new Date(2019, 2, 10);
var today = new Date();
document.getElementById("stDate").innerHTML = "target: <b>" + startDate.toDateString() + "</b>";
document.getElementById("cDate").innerHTML = "today: <b>" + today.toDateString() + "</b>";

var difference = today - startDate;
var days = difference / 1000 / 60 / 60 / 24;
var weeks = parseInt(days / 7);
var messaged = "";
if (weeks < 1) {
  message = "no weeks have passed";
} else {
  message = "weeks passed: <b>" + weeks + "</b>";
}

document.getElementById("result").innerHTML = message;
<div id="stDate"></div><br>
<div id="cDate"></div><br>
<div id="result"></div><br>

Sign up to request clarification or add additional context in comments.

8 Comments

hi thanks for your response could you explain how this code is laid out, sort new-ish js?
I'd love to help - unfortunately I didn't understand your question. What do you mean?
i guess i would like a automatic counter that starts at 1 however i don't no if this is possible to time it to start on a specific date (say 25th March 2019) then go up one number every 7 days - so the counter would change to 2 on the 1 st April 2019 (after 7 days) - does this make it any clearer?
In this case all you'd have to do is modify the startDate in my code above to reflect your target date e.g. var startDate=new Date(2019,2,25); By the way the 2 means march because months start at 0. Well there's a problem now - var weeks = parseInt(days / 7); will return a negative number since the start date is in the future - so you'd have to include some logic to compensate e.g. if(weeks<1) { // don't do whatever } else { console.log(weeks + " have passed"); }
sorry im a bit lost to what you have given me so is this correct:<!DOCTYPE html> <html> <body> <script> var startDate=new Date(2019,2,25); var weeks = parseInt(days / 7); if(weeks<1) { // don't do whatever } else { console.log(weeks + " have passed"); } </script> </body> </html> or could you show me the code in some preview mode?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.