irom
irom

Reputation: 3596

nodejs moment format can't display

below I try to convert format 17_Jul_2016 to date. I can see it is almost working but displays object instead of just date in last line (I can see that right date as last line '_d' of output)

console.log(moment("17_Jul_2016","YYYY-MM-DD"));

gives output

{
    [Number: -61611048000000]
    _isAMomentObject: true,
    _i: '17_Jul_2016',
    _f: 'YYYY-MM-DD',
    _isUTC: false,
    _pf: {
        empty: false,
        unusedTokens: [],
        unusedInput: ['_Jul_'],
        overflow: 1,
        charsLeftOver: 5,
        nullInput: false,
        invalidMonth: null,
        invalidFormat: false,
        userInvalidated: false,
        iso: false
    },
    _locale: {
        _ordinalParse: /\d{1,2}(th|st|nd|rd)/,
        ordinal: [Function],
        _abbr: 'en',
        _ordinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/
    },
    _d: Wed Aug 16 17 00: 00: 00 GMT - 0400(Eastern Daylight Time)
}

Upvotes: 1

Views: 82

Answers (2)

PrashantAjani
PrashantAjani

Reputation: 396

You can also try format to format your data like -

    console.log(moment("17_Jul_2016","DD_MMM_YYYY").format('DD/MM/YYYY HH:mm'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

This will give you output - 17/12/2019 12:45

Upvotes: 1

Tholle
Tholle

Reputation: 112787

A day with two numbers (17) is formatted as DD, a month abbreviation (Jul) is formatted as MMM, and finally, a four digit year (2016) is formatted as YYYY. The parser ignores non-alphanumeric characters, but "_" is used for clarity's sake.

console.log(moment("17_Jul_2016","DD_MMM_YYYY").toString());

Upvotes: 2

Related Questions