Moment JS日期时间比较

本文翻译自:Moment js date time comparison

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. 我正在使用moment.js格式化日期时间,这里有两个日期值,当一个日期大于另一个日期时,我想实现一种特定的功能。 I read most of their docs, but didn't find the function to achieve this. 我阅读了他们的大多数文档,但没有找到实现此功能的功能。 I know it will be there. 我知道会在那里。

This is my code: 这是我的代码:

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 {

}

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

Here, I am comparing two dates ie 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. 在这里,我正在比较两个日期,即2014-03-24T01:15:000 > 2014-03-24T01:14:000 ,期望第一个日期大于第二个日期,但是它总是转到else条件。 I don't know why? 不知道为什么


#1楼

参考:https://stackoom.com/question/1WpVw/Moment-JS日期时间比较


#2楼

You should be able to compare them directly. 您应该能够直接比较它们。

var date = moment("2013-03-24")
var now = moment();

if (now > date) {
   // date is past
} else {
   // date is future
}

 $(document).ready(function() { $('.compare').click(function(e) { var date = $('#date').val(); var now = moment(); var then = moment(date); if (now > then) { $('.result').text('Date is past'); } else { $('.result').text('Date is future'); } }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="text" name="date" id="date" value="2014-12-18" placeholder="yyyy-mm-dd"> <button class="compare">Compare date to current date</button> <br> <div class="result"></div> 


#3楼

I believe you are looking for the query functions , isBefore , isSame , and isAfter . 我相信您正在寻找查询函数 isBeforeisSameisAfter

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 . 如果是这样,请考虑差分函数 diff For example: 例如:

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

A few other things: 其他一些事情:

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

    Of course, you might as well: 当然,您也可以:

     var date_time = '2013-03-24T10:15:20:12Z'; 
  • You're using: .tz('UTC') incorrectly. 您正在使用: .tz('UTC')错误。 .tz belongs to moment-timezone . .tz属于moment-timezone You don't need to use that unless you're working with other time zones, like America/Los_Angeles . 除非您正在与其他时区一起使用,例如America/Los_Angeles ,否则不需要使用它。

    If you want to parse a value as UTC, then use: 如果要将值解析为UTC,请使用:

     moment.utc(theStringToParse) 

    Or, if you want to parse a local value and convert it to UTC, then use: 或者,如果要解析本地值并将转换为UTC,请使用:

     moment(theStringToParse).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. 仅因为输入值使用UTC,并不意味着您必须在整个函数中都使用UTC。

  • You seem to be getting the "now" instance by moment(new Date()) . 您似乎正在moment(new Date())获得“ now”实例moment(new Date()) You can instead just use moment() . 您可以改为使用moment()

Updated 更新

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

#4楼

Jsfiddle: http://jsfiddle.net/guhokemk/1/ Jsfiddle: http//jsfiddle.net/guhokemk/1/

 function compare(dateTimeA, dateTimeB) {
    var momentA = moment(dateTimeA,"DD/MM/YYYY");
    var momentB = moment(dateTimeB,"DD/MM/YYYY");
    if (momentA > momentB) return 1;
    else if (momentA < momentB) return -1;
    else return 0;
}

alert(compare("11/07/2015", "10/07/2015"));

The method returns 1 if dateTimeA is greater than dateTimeB 如果dateTimeA大于dateTimeB,则该方法返回1

The method returns 0 if dateTimeA equals dateTimeB 如果dateTimeA等于dateTimeB,则该方法返回0。

The method returns -1 if dateTimeA is less than dateTimeB 如果dateTimeA小于dateTimeB,则该方法返回-1。


#5楼

pass date to moment like this it will compare and give result. 这样传递日期到瞬间,它将进行比较并给出结果。 if you dont want format remove it 如果您不想格式化,请将其删除

moment(Date1).format("YYYY-MM-DD") > moment(Date2).format("YYYY-MM-DD")

#6楼

moment(d).isAfter(ahead30now); // true

http://momentjs.com/docs/#/query/is-after/ http://momentjs.com/docs/#/query/is-after/

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

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值