We display a column of dates and times, and use Moment.js to format the text that is displayed:

Let's assume that adate is September 9, 2013 and the time is 08:00 AM.

var formatted = moment(adate).format("L LT");

The date is perfect and is zero padded, for example: 09/22/2013

The time is not zero padded; I am getting 8:00

Is there a way to tell Moment.js to zero pad the time, so my column of dates and times looks good?

I am using version 2.1.0.

有帮助吗?

解决方案

You need to alter the longDateFormat (docs): LT: "hh:mm A" (the hh is the important bit)

moment.lang('en', {
    longDateFormat : {
        LT: "hh:mm A",
        L: "MM/DD/YYYY",
        l: "M/D/YYYY",
        LL: "MMMM Do YYYY",
        ll: "MMM D YYYY",
        LLL: "MMMM Do YYYY LT",
        lll: "MMM D YYYY LT",
        LLLL: "dddd, MMMM Do YYYY LT",
        llll: "ddd, MMM D YYYY LT"
    }
});

var d = moment("Dec 25, 1995 4:00 AM GMT")

d.format("L LT")

Result:

"12/24/1995 08:00 PM" // I'm GMT -8 so it checks out for me :) 

I haven't tested to see if you can just set the LT by itself. Seems possible/likely.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top