#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    How to display days , hours and minutes in my page which is comming from java script


    hi
    i got little problem. can you give me suggestion.
    javascript Code:
     
    <script type = "text/javascript">
    var hours = document.getElementById("totalTime").value;
    var days = Math.floor(hours/24);
    var hrs = Math.floor(hours%24);
    var decimal = hours - Math.floor(hours);
    var minutes = Math.round(decimal * 60);
     
    document.getElementById("actualDays").value= +days+" Day(s) "+hrs+" Hour(s) "+minutes+" Minute(s)";
     
    </script>


    based on above script i am able to display my values in text box like below.

    Code:
    <input  name="actualDays"  id="actualDays" value="" size="30"  readonly="yes"/>


    but i need to display these values as a text values (using lable or span....).
    can you help me for this
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    If you have a span element on your page with an id of say "mySpan" eg:
    Code:
    <span id="mySpan"></span>
    You can change the text that it displays like this:

    Code:
    document.getElementById("mySpan").innerHTML = +days+" Day(s) "+hrs+" Hour(s) "+minutes+" Minute(s)";
    The same works for labels too.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Replay


    Thank you Edge360...i will elaborate my requirement and my concern.so that you can easily understand
    my question is, i am not going to enter the total time in text box. the value of "totaltime" getting from database. so just i need to display after convert to Days ,hours, minutes.

    so that i have taken the hidden text filed for getting the "totaltime" from database using coldfusion code(server side technology)
    Code:
    <input type="hidden" name="totalTime"  id="totalTime" value="#getTime.total#">
    then i am passing these value to java script like below for calculate days hours, minutes.
    Code:
    var hours = document.getElementById("totalTime").value;

    now i need to display days hours, minutes.
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    What measurement and format is the time in?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    15 Minutes - 0.25
    30 Minutes - 0.50
    45 Minutes - 0.75
    60 Minutes - 1.00
    75 Minutes - 1.25 and so on...


    like that i calculate the total time into Database and i retrieve
    104 as totaltime
  10. #6
  11. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    That is an odd way of storing time. It would better to store time as milliseconds or seconds in my opinion. You are then able to perform relatively simple quotient and remainder calculations to split your number into units of time.

    So let's take your 104 hours and multiply it by 3600 to put it into seconds.

    Javascript Code:
    var Seconds,Minutes,Hours,Days;
     
    var dbHours = 104;
    var timeInSeconds = dbHours * 3600;
     
    var inMinutes = timeInSeconds / 60;
    Seconds = timeInSeconds % 60;
     
    var inHours = inMinutes / 60;
    Minutes = inMinutes % 60;
     
    var Days = parseInt(inHours / 24); // Just going to parse the remainder as we have our highest unit.
    Hours = inHours % 24;
     
    alert (Days+' days, '+Hours+' hours, '+Minutes+' minutes and '+Seconds+' seconds.');
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  12. #7
  13. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    I completely misread your question.
    Javascript Code:
    var daysSpan = document.getElementById('days');
    daysSpan.appendChild( document.createTextNode(days) );
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0
    using innerHTML i resolved my issue

    regards

    vamsc
  16. #9
  17. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    You can use innerHTML as it is supported by the vast majority of browsers. It is not part of the DOM standard though and it will not function correctly with certain elements.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo