最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - momentjs for checking expired timestamp - Stack Overflow

programmeradmin3浏览0评论

I have a timestamp ing from a table in utc like: 2018-02-21 23:31:49.391 I'm confident it's UTC time so I make a moment out of it.

let exp = moment(2018-02-21 23:31:49.391);

That represents an expiration date/time. I'm trying to use moment to check if that timestamp is in the past pared to the current time. I think part of my issue is that perhaps moment doesn't know that's in UTC. If so, how can i specify in the constructor?

To do this parison/validation I'm creating a new moment like let now = moment().utc();

and then paring the time like

now.isAfter(exp)

However, it keeps ing back as false for me. Is not specifying the first timestamp as UTC the issue?

I have a timestamp ing from a table in utc like: 2018-02-21 23:31:49.391 I'm confident it's UTC time so I make a moment out of it.

let exp = moment(2018-02-21 23:31:49.391);

That represents an expiration date/time. I'm trying to use moment to check if that timestamp is in the past pared to the current time. I think part of my issue is that perhaps moment doesn't know that's in UTC. If so, how can i specify in the constructor?

To do this parison/validation I'm creating a new moment like let now = moment().utc();

and then paring the time like

now.isAfter(exp)

However, it keeps ing back as false for me. Is not specifying the first timestamp as UTC the issue?

Share Improve this question asked Feb 21, 2018 at 23:47 sb9sb9 3161 gold badge8 silver badges22 bronze badges 1
  • Please include a Minimal, Complete, and Verifiable example of your code so we can more easily understand the problem. – Herohtar Commented Feb 21, 2018 at 23:56
Add a ment  | 

2 Answers 2

Reset to default 4

Try it:

var a = moment('2018-02-21 23:31:49.391');
var b = moment().utc();
var d = a.diff(b,'days');
if (d > 0){
   //a is bigger than b actual moment.
} else if (d < 0){
   //a is smaller than b actual moment.
}else{
   //a =  b
}

If it's UTC time, then

let exp = moment.utc('2018-02-21 23:31:49.391');
发布评论

评论列表(0)

  1. 暂无评论