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

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

问题描述

我想获得一个比另一个 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 日期库,例如 DatejsMoment.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').toDate();

原生 JavaScript

这就像混乱的答案,但在一行中:

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

以防万一这不明显,我们将分钟乘以 60000 的原因是将分钟转换为毫秒.

And just in case this is not obvious, the reason we multiply minutes by 60000 is to convert minutes to milliseconds.

您可能认为可以在日期上加上 24 小时以获得明天的日期,对吗?错了!

You may think you can add 24 hours to a date to get tomorrow's date, right? Wrong!

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:

const NOV = 10; //because JS months are off by one...
addMinutes(new Date(2014, NOV, 2), 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.
 * https://stackoverflow.com/a/1214753/18511
 * 
 * @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) {
  if(!(date instanceof Date))
    return undefined;
  var ret = new Date(date); //don't change original date
  var checkRollover = function() { if(ret.getDate() != date.getDate()) ret.setDate(0);};
  switch(String(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 演示.

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

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