使用 JavaScript 为日期添加小时和分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6734367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding hours and minutes to a date with JavaScript
提问by user581733
I am building a meeting calendar based on time zones around the world. My problem is how to add or subtract the time zone from the user selected date in JavaScript.
我正在根据世界各地的时区构建会议日历。我的问题是如何在 JavaScript 中从用户选择的日期中添加或减去时区。
For example, on the select form, the user will select the date from a form: I would then get the results and convert to a date as below...
例如,在选择表单上,用户将从表单中选择日期:然后我将获取结果并转换为如下日期...
var ldSelectDate = new Date(document.form1.year.value,
document.form1.month.value,
document.form1.day.value);
I would like to add 12 midnight to this object.
我想为这个对象添加午夜 12 点。
I then read in an XML that gets the time zone difference in a string between two cities: such as "+0430" for Kabul or "-0400" for New York (on daylight savings time). This is based on GMT,.
然后我读入了一个 XML,它在两个城市之间的字符串中获取时区差异:例如喀布尔的“+0430”或纽约的“-0400”(夏令时)。这是基于格林威治标准时间,。
I then calculate the time zone difference between the two cities: which will return the string of "830". (I assume I have to return this as a date object?). I got this part done returning a string. I'm working with strings.
然后我计算两个城市之间的时区差异:这将返回字符串“830”。(我假设我必须将它作为日期对象返回?)。我完成了这部分返回一个字符串。我正在处理字符串。
I then want to loop through the 24 hours of the day, set Kabul at 12 midnight and then loop through. I can most likely figure this out - that is, set the date with the whole hours as I loop.
然后我想遍历一天中的 24 小时,将喀布尔设置为午夜 12 点,然后遍历。我很可能会弄清楚这一点 - 也就是说,在我循环时用整小时设置日期。
My problem is painlessly subtract the "830" from Kabul to see what the meeting time will be in New York.
我的问题是毫不费力地从喀布尔减去“830”,看看纽约的会议时间是多少。
It will be ideal if I can just subtract the hours and the minutes from the Kabul time. I noticed on here someone subtracting the hours in JavaScript, but not the minutes. BTW, that post didn't work for me.
如果我可以从喀布尔时间中减去小时和分钟,那将是理想的。我注意到这里有人在 JavaScript 中减去了小时数,而不是分钟数。顺便说一句,那个帖子对我不起作用。
I did this with strings without the minutes, but I mess up with the minutes. There has to be an easier way.
我用没有分钟的字符串做了这个,但我搞砸了分钟。必须有更简单的方法。
I would take a solution in either native JavaScript or jQuery.
我会采用原生 JavaScript 或 jQuery 的解决方案。
Again, I need to subtract/add the time zone difference in hours and minutes to a certain date.
同样,我需要减去/添加到某个日期的小时和分钟的时区差异。
回答by Commissioner Gordon
date.setMinutes(date.getMinutes()+minutesToAdd);
The above code will set your date object ahead by the amount of minutes in the minutesToAdd variable
上面的代码将根据minutesToAdd变量中的分钟数设置您的日期对象
回答by jiggy
Easiest would be to calculate the minutes for that time delta then do a minutes delta.
最简单的方法是计算该时间增量的分钟数,然后计算分钟增量。
date.setMinutes(date.getMinutes() - (hours*60+minutes))