2017-04-23 286 views
1

我使用的NodeJS来从服务器的日期,但日期格式是yyyymmddThhmmss.SSSZ转换yyyymmddThhmmss.SSSZ格式为Unix TIMESTMP

20170423T203146.000Z 

我想这个日期字符串转换成时代时间,以轻松计算此时间与当前时间之间的差异。 (时区将始终是UTC)

但是,我找不到解析此字符串的任何可能性,因为库似乎不接受这种日期字符串。

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

回答

1

Moment.js似乎给我纠正解析日期

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

+0

谢谢,那就是我搜索的内容。 将此转换为UNIX时间戳很容易 –

1

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

它似乎对你的时间串很好地工作已:

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); 

Vi el Erfolg;)