Moment js 日期时间对比 [英] Moment js date time comparison

查看:212
本文介绍了Moment js 日期时间对比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 moment.js 来格式化我的日期时间,这里我有两个日期值,我想在一个日期大于另一个时实现特定功能.我阅读了他们的大部分文档,但没有找到实现此目的的功能.我知道它会在那里.

I'm using moment.js to format my date time, here I have two date values, and I want to achieve a particular function when one date is greater than the other. I read most of their docs, but didn't find the function to achieve this. I know it will be there.

这是我的代码:

var date_time = 2013-03-24 + 'T' + 10:15:20:12 + 'Z'
var d = moment(date_time).tz('UTC'); // first date 

var now = new Date(),
    dnow = moment(now).tz('UTC'),
    snow = dnow.minute() % 15,
    diffnow = 15 - snow,
    tonow = moment(dnow).add('minute', diffnow),
    ahead30now = moment(tonow).add('minute', 30);

if (d > ahead30now) {
    // allow input time
    console.log('UTC TIME DB', d.format());
} else {

}

编辑

var date_time = req.body.date + 'T' + req.body.time + req.body.timezone; // 2014-03-24T01:15:000
var utc_input_time = moment(date_time).utc(); // 2014-03-24T01:15:000
console.log('utc converted date_time', moment(date_time).utc().format("YYYY-MM-DDTHH:mm:SSS"));
var isafter = moment(utc_input_time).isAfter(moment('2014-03-24T01:14:000')); // true
if(isafter === true){
    console.log('is after true');
} else {
    console.log('is after is false');
}

在这里,我正在比较两个日期,即 2014-03-24T01:15:000 >2014-03-24T01:14:000,期望第一个大于第二个,但总是进入else条件.不知道为什么?

Here, I am comparing two dates i.e. 2014-03-24T01:15:000 > 2014-03-24T01:14:000, expecting that the first one is greater than the second one, but it always goes to the else condition. I don't know why?

推荐答案

我相信您正在寻找 查询函数isBeforeisSameisAfter.

I believe you are looking for the query functions, isBefore, isSame, and isAfter.

但是要确切地说出您正在尝试的内容有点困难.也许您只是想了解输入时间和当前时间之间的差异?如果是这样,请考虑差异函数diff.例如:

But it's a bit difficult to tell exactly what you're attempting. Perhaps you are just looking to get the difference between the input time and the current time? If so, consider the difference function, diff. For example:

moment().diff(date_time, 'minutes')

其他一些事情:

  • 第一行有错误:

  • There's an error in the first line:

  var date_time = 2013-03-24 + 'T' + 10:15:20:12 + 'Z'

那是行不通的.我想你的意思是:

That's not going to work. I think you meant:

  var date_time = '2013-03-24' + 'T' + '10:15:20:12' + 'Z';

当然,你也可以:

  var date_time = '2013-03-24T10:15:20:12Z';

  • 您使用的是:.tz('UTC') 错误..tz 属于 moment-timezone.您不需要使用它,除非您使用其他时区,例如 America/Los_Angeles.

  • You're using: .tz('UTC') incorrectly. .tz belongs to moment-timezone. You don't need to use that unless you're working with other time zones, like America/Los_Angeles.

    如果要将值解析为 UTC,请使用:

    If you want to parse a value as UTC, then use:

      moment.utc(theStringToParse)
    

    或者,如果您想解析本地值并将其转换为 UTC,则使用:

    Or, if you want to parse a local value and convert it to UTC, then use:

      moment(theStringToParse).utc()
    

    或许你根本不需要它.仅仅因为输入值采用 UTC,并不意味着您必须在整个函数中使用 UTC.

    Or perhaps you don't need it at all. Just because the input value is in UTC, doesn't mean you have to work in UTC throughout your function.

    你似乎得到了现在"moment(new Date()) 的实例.您可以改为使用 moment().

    You seem to be getting the "now" instance by moment(new Date()). You can instead just use moment().

    根据您的编辑,我认为您可以这样做:

    Based on your edit, I think you can just do this:

    var date_time = req.body.date + 'T' + req.body.time + 'Z';
    var isafter = moment(date_time).isAfter('2014-03-24T01:14:00Z');
    

    或者,如果您想确保您的字段被验证为正确格式:

    Or, if you would like to ensure that your fields are validated to be in the correct format:

    var m = moment.utc(req.body.date + ' ' + req.body.time, "YYYY-MM-DD  HH:mm:ss");
    var isvalid = m.isValid();
    var isafter = m.isAfter('2014-03-24T01:14:00Z');
    

    这篇关于Moment js 日期时间对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆