AndroidGeek AndroidGeek - 1 month ago 9
AngularJS Question

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).

Answer

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);