最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Moment.js: Is it possible to zero pad a time (LT)? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Sep 12, 2013 at 18:06 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Sep 12, 2013 at 18:05 A Bit of HelpA Bit of Help 1,8063 gold badges24 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论