www.webdeveloper.com
Results 1 to 4 of 4

Thread: splitting variable and concatenating string

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    3

    splitting variable and concatenating string

    Hello, I need to split the hours variable into hours and minutes and concatenate the string:

    function updateDistances(item) {
    try {
    var prev = $($(item).prevAll('li:not(.show_placeholder):first'));

    if (prev != undefined && !$(prev).hasClass('temp_placeholder')) {
    calcRoute(prev, $(item));
    }
    } catch(e) {
    log(e);
    }

    try {
    var next = $($(item).nextAll('li:not(.show_placeholder):first'));

    if (next == undefined || $(next).hasClass('temp_placeholder')) {
    $($(item).find('span.drive_time')[0]).text(hours.toString().substring(0, 4) + ' h' + ' :' + ' m');


    $($(item).find('span.drive_miles')[0]).text('0.0 mi');
    $(item).find('select.transfer').val('');
    } else {
    calcRoute($(item), next);
    }
    } catch(e) {
    log(e);
    }
    }

    The line I need to change is:

    $($(item).find('span.drive_time')[0]).text(hours.toString().substring(0, 4) + ' h' + ' :' + ' m');

    I am unsure what to do with this line of text, for instance [0] and (0,4)

    any help appreciated.

    Right now it is converting all values to hours instead of hours and minutes. Thanks,

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Is the hours variable a number, string, what?

    if it is a number (in seconds) you can do this
    Code:
    function strTime(n)
    {
        var s = (n >= 86400) ? Math.floor(n / 86400) + ' ' : '';
        // format: "d hh:mm:ss"
        return s + ((n / 3600) % 24)+ ':' +((n / 60) % 60)+ ':' +(n % 60)+ '';
    }

  3. #3
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    3

    Response to your code

    Quote Originally Posted by ShrineDesigns View Post
    Is the hours variable a number, string, what?

    if it is a number (in seconds) you can do this
    Code:
    function strTime(n)
    {
        var s = (n >= 86400) ? Math.floor(n / 86400) + ' ' : '';
        // format: "d hh:mm:ss"
        return s + ((n / 3600) % 24)+ ':' +((n / 60) % 60)+ ':' +(n % 60)+ '';
    }

    It does not return hh:mm:ss

    only hh:mm

  4. #4
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    I porting a PHP function to JavaScript (messed up the printed values)
    Code:
    function strTime(n)
    {
        var r = (n >= 86400) ? Math.floor(n / 86400) + ' ' : '', s;
        // format: "d hh:mm:ss"
    	s = Math.floor((n / 3600) % 24) + '';
    	r += (s.length < 2 ? '0' + s : s) + ':';
    	s = Math.floor((n / 60) % 60) + '';
    	r += (s.length < 2 ? '0' + s : s) + ':';
    	s = Math.floor(n % 60) + '';
    	r += (s.length < 2 ? '0' + s : s) + '';
        return r;
    }
    Code:
    console.log(strTime(5)) // "00:00:05"
    console.log(strTime(5 * 60)) // "00:05:00"
    console.log(strTime(5 * 60 * 60)) // "05:00:00"
    console.log(strTime(5 * 24 * 60 * 60)) // "5 00:00:00"
    Last edited by ShrineDesigns; 05-20-2014 at 04:47 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles