如何添加30分钟到JavaScript Date对象? [英] How to add 30 minutes to a JavaScript Date object?

查看:119
本文介绍了如何添加30分钟到JavaScript Date对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得比另一个Date对象晚30分钟的Date对象。如何使用JavaScript?

I'd like to get a Date object which is 30 minutes later than another Date object. How do I do it with JavaScript?

推荐答案

使用库



如果您正在做很多日期工作,您可能需要查看JavaScript日期库,例如 Datejs 或<一个href =http://momentjs.com/ =noreferrer> Moment.js 。例如,使用Moment.js,这只是简单的:

Using a Library

If you are doing a lot of date work, you may want to look into JavaScript date libraries like Datejs or Moment.js. For example, with Moment.js, this is simply:

var newDateObj = moment(oldDateObj).add(30, 'm');



香草JavaScript



这就像< a href =http://stackoverflow.com/a/1197939/18511>混乱的答案,但在一行中:

var newDateObj = new Date(oldDateObj.getTime() + diff*60000);

其中 diff 是分钟数的差异想从 oldDateObj 的时间。它可以是否定的。

Where diff is the difference in minutes you want from oldDateObj's time. It can even be negative.

或者作为可重用的功能,如果您需要在多个地方执行此操作:

Or as a reusable function, if you need to do this in multiple places:

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}



小心使用香草Javascript。日期很辛苦!



您可能认为您可以添加24小时的日期来获取明天的日期,对吧?

addMinutes(myDate, 60*24); //DO NOT DO THIS

事实证明,如果用户观察夏令时,一天是不一定24小时。每年有一天只有23个小时,每年有一天是25个小时。例如,在大多数美国和加拿大,2014年11月2日午夜24小时后,仍然是11月2日:

It turns out, if the user observes daylight saving time, a day is not necessarily 24 hours long. There is one day a year that is only 23 hours long, and one day a year that is 25 hours long. For example, in most of the United States and Canada, 24 hours after midnight, Nov 2, 2014, is still Nov 2:

addMinutes(new Date('2014-11-02'), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!

这就是为什么使用上述库之一是一个更安全的赌注如果你需要做很多工作。

This is why using one of the afore-mentioned libraries is a safer bet if you have to do a lot of work with this.

下面是我写的这个函数的一个更通用的版本。我仍然建议使用图书馆,但这可能是您的项目过度/不可能。语法在 MySQL DATE_ADD之后建模功能。

Below is a more generic version of this function that I wrote. I'd still recommend using a library, but that may be overkill/impossible for your project. The syntax is modeled after MySQL DATE_ADD function.

/**
 * Adds time to a date. Modelled after MySQL DATE_ADD function.
 * Example: dateAdd(new Date(), 'minute', 30)  //returns 30 minutes from now.
 * 
 * @param date  Date to start with
 * @param interval  One of: year, quarter, month, week, day, hour, minute, second
 * @param units  Number of units of the given interval to add.
 */
function dateAdd(date, interval, units) {
  var ret = new Date(date); //don't change original date
  var checkRollover = function() { if(ret.getDate() != date.getDate()) ret.setDate(0);};
  switch(interval.toLowerCase()) {
    case 'year'   :  ret.setFullYear(ret.getFullYear() + units); checkRollover();  break;
    case 'quarter':  ret.setMonth(ret.getMonth() + 3*units); checkRollover();  break;
    case 'month'  :  ret.setMonth(ret.getMonth() + units); checkRollover();  break;
    case 'week'   :  ret.setDate(ret.getDate() + 7*units);  break;
    case 'day'    :  ret.setDate(ret.getDate() + units);  break;
    case 'hour'   :  ret.setTime(ret.getTime() + units*3600000);  break;
    case 'minute' :  ret.setTime(ret.getTime() + units*60000);  break;
    case 'second' :  ret.setTime(ret.getTime() + units*1000);  break;
    default       :  ret = undefined;  break;
  }
  return ret;
}

工作jsFiddle演示

这篇关于如何添加30分钟到JavaScript Date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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