nihi
nihi

Reputation: 5

Javascript time manipulation retrieving hours and minutes

I made a countdown checking for hours for my website. I would like to change my check which is actually pretty basic to check not only hours but minutes too.

var currentHour = new Date().getHours();
if (currentHour >= 9 && currentHour <= 12) { do whatever } 

I would like to check not between 9h and 12h but between 9h30 and 12h30.

I can't find a way to do this with JS (i started JS yesterday but i have exp in other languages).

My first idea was to use Date() with prototype parameters to retrieve hours and minutes like this :

But can't find a way to do this and i'm sure there is another simple way to do this.

Upvotes: 0

Views: 155

Answers (2)

Shiva Sai
Shiva Sai

Reputation: 463

var today = new Date()

var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 9, 30, 0).getTime();
var endDate = new  Date(today.getFullYear(), today.getMonth(), today.getDate(), 16, 30, 0).getTime();

console.log(today.getTime()>startDate&&today.getTime()<endDate)

Upvotes: 2

Xiko
Xiko

Reputation: 44

i think you could do something like this

const startDate = new Date('month day, year 09:30:00');
const endDate = new Date('month day, year 12:30:00');
var currentDate = new Date(your date);

if(currentDate > startDate && currentDate < endDate){
your code
}

Upvotes: 0

Related Questions