Leading 0 missing from data and time

后端 未结 3 1836
渐次进展
渐次进展 2020-12-21 16:06

I have a function which works well, for converting dates from a webservice returned in json format. The webservices gives dates in the following type of format:

相关标签:
3条回答
  • 2020-12-21 16:36

    You could also try moment.js. A 6.5kb library for formatting dates

    var m = moment( new Date() );
    m.format( "DD/MM/YYYY HH:mm");
    
    0 讨论(0)
  • 2020-12-21 16:37

    If you don't use a library, then you have to do some work, that is you have to put the "0" yourself.

    Instead of simply concatenating day, you need to concatenate

    (day<10 ? '0'+day : day)
    

    and the same for the other fields.

    But note that there are good javascript libraries filling this kind of gap. I personally used datejs for date manipulations.

    0 讨论(0)
  • 2020-12-21 16:43

    I'd suggest using a library for this kind of thing -- something like Moment.js would do the job perfectly (and give you a load more functionality like date addition/subtraction into the bargain).

    With moment.js, your code could look like this:

    function HumanDate(date) {
        return moment(date).format('MM/DD/YYYY HH:mm');
    }
    

    usage example:

    alert(HumanDate("\/Date(1373875200000)\/"));
    //alerts "07/15/2013 09:00"
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题