Q: Javascript - How to check whether the current time is between two dates considering the time irrespective of year I want to implement a functionality in JavaScript. I want to set different images as background to the webpage during Christmas week and this should happen every year. How can I find whether the given time is between two date and time intervals? (Say I want to display Christmas images from 24th Dec 4PM to 26 Dec 10 AM by not considering the year in the date).

A: Just use the date javascript object to asign the dates between you want to be.


var today = new Date();

var fakeToday = new Date(today.getFullYear(), 11, 24, 20, 00) //fake date to check

var xMasInit = new Date(today.getFullYear(), 11, 24, 16, 00) // 24 december at 16pm of currentyear
var xMasEnd = new Date(today.getFullYear(), 11, 26, 10, 00); //26 december at 10am of currentYear

var isXmas = xMasInit <= today && today <= xMasEnd
var fakeXmas =  xMasInit <= fakeToday && fakeToday <= xMasEnd

console.log(isXmas);
console.log(fakeXmas);



A: Use following code
`
  var t1 = new Date();
  var t2 = new Date();
  t1.setYear(2000);
  t2.setYear(2000)
  string result = "";
  if(t1.getTime()>t2.getTime())
    result = "t1 is greater";
  else
    result = "t2 is greater";

`

A: var dt = new Date();
var date = dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" +  dt.getDate();
var start = dt.getFullYear() +"/"12/04";
var end = dt.getFullYear() +"/"12/26"

if(date <= start || date >= end)
{
  $('#background').css('background','newImage.jpg')
}


A: Here it is :)
var d = new Date()
var curMonth = d.getMonth() + 1;
var curDate = d.getDate();
var curHours = d.getHours();

if (
    (curMonth == 12 && (curDate > 24 || curDate == 24 && curHours > 16)) && 
    (curMonth == 12 && (curDate < 26 || curDate == 26 && curHours < 10))
   )
console.log('Christmas'); 
else
console.log('Not Christmas');

