-1

This is my first time posting a question on here however I am a long term user of StackOverflow.

I am very new to Javascript and am currently trying to solve a few issues I am having on a website I am creating for my university course.

I am trying to make a little information box on the website's dashboard that simply tells you when your next rubbish bin collection day is. This is currently every 2 weeks on a Friday, the next being 1st April. I was wondering if there was a simple way for me to display the next bin collection date until it's the day of the bin collection, where the text would change to say 'bin collection today!' and after about 6pm it would change to the next collection date in a fortnight.

Sorry if this is extremely poorly worded! The website will only be used in the UK so I don't need to worry about time zones.

Any help would be very much appreciated!

Many thanks, Emily

Emily
  • 5
  • 6
  • please see http://stackoverflow.com/help/how-to-ask and display what you alredy discovered / tested – profesor79 Mar 19 '16 at 21:38
  • You might start with creating a Date object with `new Date()`. You can determine the day with the *getDay* method (5 is Friday). You also need some epoch for which Friday is the start of the fortnightly cycle. – RobG Mar 20 '16 at 09:46

2 Answers2

0

A possible solution is to use Unix epoch, because they give you your time in seconds from 1/1/1970. By using this we can find how far we are into a fortnight. I found that Math.floor(Date.parse("2016-04-01")/86400000)%14equals 8.

Then your code might be:

var days_into_fortnight=Math.floor(Date.now()/86400000)%14;
var string_to_show;
var days_to_collection;
if (days_into_fortnight==8){
    string_to_show="Bin collection!";
}
else{
    if(days_into_fortnight<8){
        days_to_collection=8-days_into_fortnight;
    }
    else{
        days_to_collection=22-days_into_fortnight;
    }
    string_to_show=days_to_collection.toString()+" day(s) to collection!";
}

Edit:spelling

  • This measures 14 day periods since 1 January, 1970 GMT which was a Thursday. The OP wants local Friday, without saying which timezone that should be. It also assumes that the fortnightly cycle coincides with 2 January when it might be 9 January 1970. – RobG Mar 20 '16 at 09:12
0

Below is some code to show how it might be done, hopefully there are sufficient comments.

As far as I know, some parts of the UK observe daylight saving but the following should not be affected by any change of timezone since it uses local date methods.

Any date can be used for the start of the cycle, I've chosen 10 June 2011 totally randomly. Also, any time of day can be used to change the message on the final day from "put your bins out today" to the standard "next cycle is in x days".

The algorithm is calculate the number of milliseconds to the next full fortnight from the start date by subtracting the number of milliseconds from the last full fortnight from the milliseconds per fortnight. Then the remainder is converted to full days.

To save ambiguity on the day before the cycle ends (in this case, a Thursday) the message says the cycle ends "tomorrow" rather than in 1 day, and on the day itself, up to 18:00 or 6 pm it says the cycle ends today. After that, it says the cycle ends in 14 days or less.

// Date objects are based on milliseconds (8.64e7 per day)
// Calculate milliseconds for a fortnight
var msPerFortnight = 8.64e7 * 14;

// Any date can be the start of the fortnightly cycle
// Make Friday 10 June 2011 first day of cycle,
// Cycles end on 25 March, 6 April 2016.
// Note months are zero based so June is 5
var firstDayOfCycle = new Date(2011, 5, 10);

// Time of day to stop showing "put bins out today" message on start/end day
// 18 is 6 pm
var endHour = 18;

// Get the day name for the cycle start/end
var dayName = ['Sunday','Monday','Tuesday','Wednesday','Thursday',
               'Friday','Saturday'][firstDayOfCycle.getDay()];

// Convert end hour to convenient format with am/pm
var endHourNeat = (endHour % 12 || 12) + ' ' + (endHour < 12? 'am' : 'pm');

// Get a date for now
var now = new Date();

// Get milliseconds to next full fortnight by
// msPerFortnight minus milliseconds since last full fortnight
var m = msPerFortnight - ((new Date() - firstDayOfCycle) % msPerFortnight);

// Calculate time remaining full days
var daysLeft  = Math.ceil(m / 8.64e7);

// Create a human friendly message
var message;

// If more then one day left, or after 18:00 on last day, show this message
if (daysLeft == 14 && now.getHours() < endHour) {
  message = 'Today is ' + dayName + ', so please put your bins out before ' + endHourNeat + '!';
} else if (daysLeft > 1 ) {
  message  = 'Put your bins out on ' + dayName + ' in ' + daysLeft +
             ' day' + (daysLeft == 1?'':'s') + ' time.';
} else if (daysLeft == 1) {
  message = 'Put your bins out tomorrow, on ' + dayName + '.';
}

document.write(message);

You can even add the date of the end of the cycle and add ordinal, so it might read "Put your bins out on Friday the 25th, in 3 days time". But I'll leave that up to you.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thank you so much for your help! That seems to be working, I would like to add the date, ie Friday 25 March, it doesn't necessary have to have an ordinal, how easy would that be to add? – Emily Mar 20 '16 at 16:41
  • @Emily—you can get the date of the end of the cycle by adding *daysLeft* days to *now*, it's the same principle as [*adding one day to a date*](http://stackoverflow.com/questions/9989382/add-1-to-current-date/9989458#9989458). There are lots of questions and answers on [*formatting dates*](http://stackoverflow.com/search?q=%5Bjavascript%5D+format+date), find one that suits. – RobG Mar 20 '16 at 23:23
  • Thank you so much, really appreciate your help! – Emily Mar 21 '16 at 12:27