SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Orange County, CA, Unites States
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writing a code to countdown hours and minutes until tomorrow

    Hey Everyone,

    So I'm trying to write a script to countdown the number of hours and minutes until tomorrow. So far, this is what I have:

    Code:
    <%
    response.write(dateDiff("h",now(),date+1)) & " hours and " & dateDiff("m",Time(),date+1) & " Minutes "
    %>
    The code above correctly displays the hours until tomorrow... but it's not displaying the minutes correctly. It's displaying the total minutes until tomorrow. If there at 4 hours left until tomorrow it's saying "4 Hours and 240 minutes."

    I need it to display "4 hours and 0 minutes."

    The best way I think this can be done is to take the current time in minutes and subtract that from 60. What you have left is the total minutes until the next hour.

    So... can someone help me figure our how to take the current time in minutes and subtract that from 60?

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First point to make is that "m" returns months in datedif. You need to use "n" to return minutes.

    I think this will do what you want:
    Code:
    <&#37; diff_in_minutes = dateDiff("n",now(),date+1)
    response.write(Cstr(diff_in_minutes\60) + " hours and " + Cstr(diff_in_minutes mod 60) + " minutes")
    %>

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Location
    Orange County, CA, Unites States
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    First point to make is that "m" returns months in datedif. You need to use "n" to return minutes.

    I think this will do what you want:
    Code:
    <% diff_in_minutes = dateDiff("n",now(),date+1)
    response.write(Cstr(diff_in_minutes\60) + " hours and " + Cstr(diff_in_minutes mod 60) + " minutes")
    %>
    Thanks Reggie,

    I had been playing around with this all day today and I finally got it working . Thank you for the help.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you spot that:
    Code:
    Cstr(diff_in_minutes mod 60)
    returns a fraction of an hour? This bit needs to be:
    Code:
    Cstr(60*(diff_in_minutes mod 60))
    I slept on it and realised I'd made a mistake.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •