当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Invalid time when using custom format momentJS

问题描述:

Hello I am trying to calculate the difference between a creationdatetime (using the format yyyyMMddhhmm) of a chatroom and the current time. I'm doing this for a web application so I am using moment js for this. However when i try to parse the datetime from the server (creationtime) and do some calculations with it I get back invalid time my question is where do I go wrong and how do i fix this so i get the time difference back. My code is as follows

var roomnamearr = roomName.split('~');

var creationtime = moment(roomnamearr[0],"yyyyMMddhhmm").format("DD/MM/YYYY HH:mm");

var difference = moment(creationtime).fromNow();

return difference ;

网友答案:

You got good advice in the comments regarding the tokens, but note that there's no need to be calling to moment twice in this code. You can simply write:

var roomnamearr = roomName.split('~');
return moment(roomnamearr[0],"YYYYMMDDHHmm").fromNow();  
分享给朋友:
您可能感兴趣的文章:
随机阅读: