简体   繁体   中英

Moment.js not parsing javascript date correctly

I'm getting a date from SQL Server which then passes through ASP.NET. I'm iterating through a list of objects in jQuery and want to display some dates along with other data. However, no matter what time is coming from the database (a variety of date/times from earlier), the minutes always read 10. What's going on?

Here's some web browser console output. These were all entered at different minutes past the hour, but they all say 10 past.

this.CreatedTime = /Date(1507924194433)/
moment-formatted = 10/13/17 12:10pm
this.CreatedTime = /Date(1507924096347)/
moment-formatted = 10/13/17 12:10pm
this.CreatedTime = /Date(1507923917443)/
moment-formatted = 10/13/17 12:10pm
this.CreatedTime = /Date(1507923693083)/
moment-formatted = 10/13/17 12:10pm
this.CreatedTime = /Date(1507667688047)/
moment-formatted = 10/10/17 1:10pm
this.CreatedTime = /Date(1507667348210)/
moment-formatted = 10/10/17 1:10pm

Here is the code:

$.each(data, function () {
   console.log("this.CreatedTime = " + this.CreatedTime);
   console.log("moment-formatted = " + moment(this.CreatedTime).format("MM/DD/YY h:MMa"));
});

Here are the actual dates from SQL Server:

2017-10-13 12:49:54.433
2017-10-13 12:48:16.347
2017-10-13 12:45:17.443
2017-10-13 12:41:33.083
2017-10-10 13:54:04.227
2017-10-10 13:34:48.047

Try changing the format to

MM/DD/YY h:mma
           ^^

MM is for months, mm is for minutes.

I think that the 10 being returned is the month. in moment MM is month. You want to use mm .

你的日期格式不正确,请尝试moment(this.CreatedTime).format("MM/DD/YY h:mma")

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