JavaScript: IF time >= 9:30 then

I'm trying to write a statement that says "if time is this and less than that then". I can use get hours and get min. However, I'm having problems combining a time such as 9:30.

Example,

var now = new Date();
var hour = now.getHours();
var day = now.getDay();
var mintues = now.getMinutes();

if (day == 0 && hour >= 9 && hour <= 11 && mintues >= 30) { 
    document.write(now); 
}

This only if the time is less between 9:30 10. As soon as the clock hits 10 the minutes are then < 30 and the script breaks.

Any thoughts on how to better incorporate the time function to make this theory work?

Thanks,

Answers:

Answer

use new Date().getTime() returns milliseconds for much easier comparison. This way there is no need to check hour, min, second, millisecond. Fiddle link

var d930 = new Date(2010, 12, 21, 9, 30, 0, 0), // today 9:30:00:000
    d931 = new Date(2010, 12, 21, 9, 31, 0, 0), // today 9:31:00:000
    t930 = d930.getTime(),
    t931 = d931.getTime();


console.log(t931 > t930);

This way your code can check against a static 9:30 time.

var time930 = new Date(2010, 12, 21, 9, 30, 0, 0).getTime(),
    sunday = 0,
    now = new Date();

if(now.getDay() == sunday && now.getTime() >= time930){
    /* do stuff */
}
Answer

You have a few typos and basic javascript errors.
Might wanna brush up on the basics.
W3Schools is where I learned all I know. It works fine if you fix them...

var now = new Date();
  var hour = now.getHours();
  var day = now.getDay();
  var minutes = now.getMinutes();
  if(day == 0 && hour == 9 && minutes < 30 && minutes > 10 || day == 0 && hour == 9)
      document.write('Time is between 9:10 and 9:30');

Think of the if statement as basic logic.
If the day is Sunday(0)
AND the hour is 9
AND the minutes are greater than 10 AND the minutes are less than 10 OR the day is Sunday(0)
AND the hour is before 9.

Answer
  var now = new Date();
  var closeTime = new Date();
  closeTime.setHours(9); closeTime.setMinutes(30);
  console.log(now, closeTime, now.getTime() >= closeTime.getTime());

close time is based on today, then we just change the hours and minutes to 9:30.

Answer
if the hour is less than 9, true
or
if hour is 9 and minutes lt 30, true

so that would look like

if ((hour < 9) || (hour == 9 && minutes < 30))

Use words to figure out your logic. Symbols are just shortcuts.

Answer

One way is to do a direct comparison on date objects. Choose an arbitrary year, month and day, and then incorporate your times as follows:

var older = new Date("1980-01-01 12:15");
var newer = new Date("1980-01-01 12:30");

if (newer > older){
    alert("Newer time is newer");

} else {
    alert ("The time is not newer");

}

The MDC documentation on the Date object will help with some more details. The bottom line is that if you want to compare times, you don't actually need to call any methods on the objects, and it's possible to directly compare them. The date() object can take a variety of strings to assign a new time to the returned instance, these are from the MDC documentation:

today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);

As you can see, it's pretty simple. Don't complicate, and have a look through the documentation :)

While we're here, here's a test using your example:

var base = new Date("1980-01-01 9:30");
var test = new Date("1980-01-01 9:30:01");

if (test >= base){
    alert("test time is newer or equal to base time");

} else {
    alert ("test time is older than 9.30");

}
Answer

Try this:

var now = new Date(); 
var hour = now.getHours(); 
var mintues = now.getMinutes();

if(
        (hour*60 + mintues) > 570 && 
        hour <= 11
    )
{
    document.write(now); 
}
Answer

I don't quite fully understand your question but hope this helps.

c = new Date();
nhour  = c.getHours();
nmin   = c.getMinutes();

if(nmin <= 9) {
    nmin = "0" + nmin;
}
if(nhour <= 9) {
    nhour = "0" + nhour;
}

newtime = nhour + "" + nmin;

if(newtime <= 0930){
    alert("It is before 9:30am or earlier");
}
Answer

I made this solution simple and easy to read (thus easy to adjust) by using a simple trick.

The trick is to transform the hour and minutes into a number. Eg 09:30 into 9.30

var now     = new Date();
var hour    = now.getHours();
var minutes = now.getMinutes();

// make the current time of day computable and readable at the same time
// 9:30 becomes 9.30
var timeOfDay = hour + (minutes / 100);

console.log('timeOfDay: ' + timeOfDay);

// For setting the time limits simply use the time with a dot instead of a colon
if( (9.30 <= timeOfDay) && (timeOfDay <= 11.30) ){
    console.log('Time is between 9:30 and 11:30');
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.