0

How to format a string in jQuery. i have a json code need to format like this,

    "OrigDests": [
            {
                "TotalEllapsedTime": "1.03:45:00",
                "Segments": [
                    {...

1.03:45:00 ; i want to replace "." sign into "days" and format the other into hh:mm:ss;

Arturs
  • 1,258
  • 5
  • 21
  • 28

4 Answers4

0

Try

var str="1.03:45:00";
var value = str.replace(".", "day ");
alert(value);

http://jsfiddle.net/VbjKr/

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
  • thanks sir, sir 1 more question; is it possible that this "1.03:45:00" can split and print something like this 1day 3hrs 45mins 0sec – user2731440 Aug 30 '13 at 05:24
0

DEMO

var x = "1.03:45:00".split('.');
var y = x[0] + ' Day';
var z = x[1].split(':');
var str = y + ' ' + z[0] + 'hours ' + z[1] + 'minutes ' + z[2] + 'second';
console.log(str); //1 Day 03hours 45minutes 00second 
alert(str); //1 Day 03hours 45minutes 00second 

.split

json.stringify()

Serializing to JSON in jQuery

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

The best way to use a format is javascript is to write your own.

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined' ? args[number] : match;
    });
};

Now after this, formatting can be done like

var formatted = "{0} days {1}".format(elapsedTime.substring(0, elapsedTime.indexOf('.')), elapsedTime.substring(elapsedTime.indexOf('.') + 1));

You can see the example here

achakravarty
  • 418
  • 3
  • 7
0

The best way is to use replace function :

var str = "1.03:45:00";
str = str.replace('.','day');
Prateek
  • 12,014
  • 12
  • 60
  • 81