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

这是我的代码:

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条件。我不知道为什么?


当前回答

你应该能够直接比较它们。

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>

其他回答

var startDate = moment(startDateVal, "DD.MM.YYYY");//Date format
var endDate = moment(endDateVal, "DD.MM.YYYY");

var isAfter = moment(startDate).isAfter(endDate);

if (isAfter) {
    window.showErrorMessage("Error Message");
    $(elements.endDate).focus();
    return false;
}

对于日期-时间比较,你可以使用时刻的valueOf函数,它提供日期-时间的毫秒数,最适合进行比较:

const date1 = moment('01-02-2020', 'DD-MM-YYYY').valueOf() const date2 = moment('11-11-2012', 'DD-MM-YYYY').valueOf() // console.log(date1 > date2 ?'date1': 'date2') + "是更大的…") If (date1 > date2) { Console.log ("date1 is greater…") }其他{ Console.log ("date2 is greater…") } < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js " > < /脚本>

你应该能够直接比较它们。

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>

我相信您正在寻找查询功能,isBefore, isSame, isAfter。

但要确切地说出你在尝试什么有点困难。也许你只是想知道输入时间和当前时间之间的差异?如果是,考虑差分函数diff。例如:

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'; Of course, you might as well: var date_time = '2013-03-24T10:15:20:12Z'; 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. If you want to parse a value as UTC, then use: moment.utc(theStringToParse) Or, if you want to parse a local value and convert it to UTC, then use: 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. You seem to be getting the "now" instance by moment(new Date()). You can instead just use moment().

更新

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

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

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

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

重要的是,你的datetime在正确的ISO格式使用任何momentjs查询:isBefore, isAfter, isSameOrBefore, isSameOrAfter, isBetween

所以你的日期时间应该是:

2014-03-24T01:14:00或2014-03-24T01:14:00. 000z

否则,您可能会收到以下弃用警告,条件将计算为false:

弃用警告:所提供的值不在可识别的RFC2822或 ISO格式。moment构造返回到js Date(),这不是 适用于所有浏览器和版本。非RFC2822/ISO日期 格式是不鼓励的,并将在即将到来的主要删除 释放。详情请参阅 http://momentjs.com/guides/#/warnings/js-date/获取更多信息。

// https://momentjs.com/docs/#/query/ const dateIsAfter = moment('2014-03-24T01:15:00.000Z').isAfter(moment('2014-03-24T01:14:00.000Z')); const dateIsSame = moment('2014-03-24T01:15:00.000Z').isSame(moment('2014-03-24T01:14:00.000Z')); const dateIsBefore = moment('2014-03-24T01:15:00.000Z').isBefore(moment('2014-03-24T01:14:00.000Z')); console.log(`Date is After: ${dateIsAfter}`); console.log(`Date is Same: ${dateIsSame}`); console.log(`Date is Before: ${dateIsBefore}`); <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.min.js" ></script>