1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Display time every 15 min with js

Discussion in 'JavaScript' started by Philip C. Ngo, Aug 10, 2014.

  1. #1
    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)
     
    Philip C. Ngo, Aug 10, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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.
     
    PoPSiCLe, Aug 10, 2014 IP
  3. Philip C. Ngo

    Philip C. Ngo Member

    Messages:
    85
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    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)
     
    Philip C. Ngo, Aug 10, 2014 IP
  4. Philip C. Ngo

    Philip C. Ngo Member

    Messages:
    85
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    for 24H it'll have so many if else and I want the Cleverest method with Lowest code writing
     
    Philip C. Ngo, Aug 10, 2014 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    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
     
    PoPSiCLe, Aug 10, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Divide, truncate, multiply. One-liner, no-brainer.

    d2.setMinutes(Math.floor(d1.getMinutes() / 15) * 15);
     
    deathshadow, Aug 12, 2014 IP