简体   繁体   中英

Moment js not converting the date to desired format

I have this arrray of objects:

[ 
  { id: '1', name: 'sam', entryTime: 2018-10-30T10:48:29.286Z }, 
  { id: '2', name: 'john', entryTime: 2018-10-30T11:37:23.874Z } 
]

My Expected output:

[ 
  { id: '1', name: 'sam', entryTime: 30 Oct 2018 }, 
  { id: '2', name: 'john', entryTime: 30 Oct 2018 } 
]

Here what i tried:

const visitors = [..that above array]

visitors.map(index => index.entryTime = moment(index.entryTime).format('D MMM YYYY'));

Getting invalid date in my object.

convert it in date object, your date also contain time. (Date and time is separated with a capital T).

 var visitors  = [ 
      { id: '1', name: 'sam', entryTime: '2018-10-30T10:48:29.286Z' }, 
      { id: '2', name: 'john', entryTime: '2018-10-30T11:37:23.874Z' } 
    ];

    visitors.map(index => index.entryTime = moment(new Date(index.entryTime)).format('Do MMM YY'));

实际上我的代码有效,它是 sequelize ORM 原始数据问题。

visitors.map(index => index.entryTime = moment(index.entryTime).format('D MMM YYYY'));

Try this for converting date as string to particular format.

var d = "2018-10-30T10:48:29.286Z";
var output = moment(d,'DD MMM YYYY');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM