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

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

问题描述

我使用时刻js格式化我的日期时间,这里我有两个日期值,当一个日期大于其他日期时,我希望实现一个特定的功能。我读了他们的大部分文档,但没有找到实现这一点的功能。我知道它会在那里,请帮我找出来,谢谢

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

这是我的代码

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 {

}

/ strong>

Edit

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,第一个要比第二个要大,但总是去别的条件。我不知道为什么?

Here am comparing two dates ie 2014-03-24T01:15:000> 2014-03-24T01:14:000, expecting first one is greater than second one, but it always go to else condition. I dont know why ?

推荐答案

我相信你在寻找查询功能 isBefore isSame isAfter

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 属于时刻区。您不需要使用它,除非您正在使用其他时区,例如 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,请使用: / p>

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

    moment(theStringToParse).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');
    

    或者,如果您想确保您的字段验证格式正确: / p>

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

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

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