How to display time regularly with js? I want to display time when you're refreshing web page See the time with delay if the time is 14:30 and you refreshed the page you see 14:15 and again The second time at 15:00 o'clock you see 14:45 (fifteen minute delay in each refresh)
Do a time-display, subtract 15 minutes. Check to see if the current time is more than 15 minutes later than the displayed time, if so, update with new time. Depending on whether you just want 15 minutes (14.03,14.18,14.33,14.48,15.03), or just quarter-intervals (14.00, 14.15, 14.30, 14.45,15.00) it will be a bit more or less calculating the correct time.
Let me be clear I should get the user's local time and I have a trick all of countries Time differences are 30 min and I can add this snippet: var d1 = new Date (), d2 = new Date ( d1 ); d2.setMinutes ( d1.getMinutes() - 15 ); document.write ( d2 ) it work's very well but the problem time isn't A quarter of an hour My goal is Just display time in quarter of an hour : if the time is 14:00 - 14:15 => Result => 14:00 if the time is 14:15 - 14:30 => Result => 14:15 if the time is 14:30- 14:45 => Result => 14:30 And so on... (I know that add a if else statement to this snippet)
var minutes = d1.getMinutes(); If (minutes < 15) { minutes = 00;} If (minutes > 15 && minutes < 30) { minutes = 15} And si forth and so on. You only need 5 or so ifs
Divide, truncate, multiply. One-liner, no-brainer. d2.setMinutes(Math.floor(d1.getMinutes() / 15) * 15);