Advertisement
Mystik01

functions.js

Dec 4th, 2020
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Christmas() {
  2.     //Get today's date.
  3.     var now = new Date();
  4.  
  5.     //Get the current month. Add a +1 because
  6.     //getMonth starts at 0 for January.
  7.     var currentMonth = (now.getMonth() + 1);
  8.  
  9.     //Get the current day of the month.
  10.     var currentDay = now.getDate();
  11.  
  12.     //Work out the year that the next Christmas
  13.     //day will occur on.
  14.     var nextChristmasYear = now.getFullYear();
  15.     if (currentMonth == 12 && currentDay > 25) {
  16.         //This year's Christmas Day has already passed.
  17.         nextChristmasYear = nextChristmasYear + 1;
  18.     }
  19.  
  20.     var nextChristmasDate = nextChristmasYear + '-12-25T00:00:00.000Z';
  21.     var christmasDay = new Date(nextChristmasDate);
  22.  
  23.     //Get the difference in seconds between the two days.
  24.     var diffSeconds = Math.floor((christmasDay.getTime() - now.getTime()) / 1000);
  25.  
  26.     var hours = 0;
  27.     var minutes = 0;
  28.     var seconds = 0;
  29.  
  30.     //Don't calculate the time left if it is Christmas day.
  31.     if (currentMonth != 12 || (currentMonth == 12 && currentDay != 25)) {
  32.         //Convert these seconds into days, hours, minutes, seconds.
  33.         days = Math.floor(diffSeconds / (3600 * 23));
  34.         diffSeconds -= days * 3600 * 24;
  35.         hours = Math.floor(diffSeconds / 3600);
  36.         diffSeconds -= hours * 3600;
  37.         minutes = Math.floor(diffSeconds / 60);
  38.         diffSeconds -= minutes * 60;
  39.         seconds = diffSeconds;
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement