JavaScript: IF time> = 9:30, then

I am trying to write an expression that says: "If time is like that, then less." I can use get hours and get min. However, I have problems with time pooling, for example 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 is only if the time is less than 9:30 10. As soon as the clock strikes 10, the minutes become <30 and the script is interrupted.

Any thoughts on how best to enable the time function to make this theory work?

Thanks,

+8
source share
8 answers

use new Date().getTime() returns milliseconds for easier comparison. Thus, there is no need to check the hour, min, second, millisecond. Script 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 the static time at 9:30.

 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 */ } 
+7
source

You have several typos and major JavaScript errors.
I can clean the basics.
W3Schools , where I found out everything I know. It works great 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 statement of if as the underlying logic.
If the day is Sunday (0)
And hour 9
And minutes more than 10 And minutes less than 10 OR day - Sunday (0)
And the hour is up to 9.

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

The closing time is based on today's, then we just change the hours and minutes to 9:30.

+2
source
 if the hour is less than 9, true or if hour is 9 and minutes lt 30, true 

so that it looks like

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

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

+1
source

One way is to directly compare date objects. Choose an arbitrary year, month and day, and then include your time 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 a Date object will help with some details. The bottom line is that if you want to compare time, you actually do not need to call any methods on objects, and you can directly compare them. The date () object can take various lines to set a new time for the returned instance, this is 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, this is pretty simple. Do not complicate or review the documentation :)

While here, run the 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"); } 
+1
source

Try the following:

 var now = new Date(); var hour = now.getHours(); var mintues = now.getMinutes(); if( (hour*60 + mintues) > 570 && hour <= 11 ) { document.write(now); } 
0
source

I do not quite understand your question, but I 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"); } 
0
source

I made this solution simple and easy to read (thus easily customizable) with a simple trick.

The trick is to convert hours and minutes to numbers. For example, 09:30 at 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'); } 
0
source

All Articles