Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

javascript - 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?

(不知道为什么)

  ask by Dibish translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...