将yyyymmddThhmmss.SSSZ格式转换为Unix Timestmp

我使用NodeJS从服务器获取date,但date格式是yyyymmddThhmmss.SSSZ

20170423T203146.000Z 

我试图将这个datestring转换为纪元时间,以便轻松计算此时间与当前时间之间的差异。 (时区将始终是UTC)

然而,我找不到任何parsing这个string的可能性,因为库似乎不接受这种datestring。

有人可以帮我解决这个问题吗?

Moment.js似乎给了我正确的parsingdate

 var moment = require('moment') var date = moment("20170423T203146.052Z" , "YYYYMMDDThhmmss.SSS") console.log(date.format("YYYY MM DD hh mm ss SSS")) 

产出: 2017 04 23 08 31 46 052

你应该看看Moment.js String + Format + Special Format

它已经在你的时间string上工作得很好了:

 const moment = require("moment"); const werner = "20170423T203146.000Z"; console.log(moment.utc(werner).format()); const epochSec = moment.utc(werner).unix(); const epochMs = moment.utc(werner).valueOf(); console.log(epochSec, epochMs); 

Viel Erfolg;)