使用javascript将日期和分钟添加到日期 [英] Adding hours and minutes to a date with javascript

查看:93
本文介绍了使用javascript将日期和分钟添加到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据世界各地的时区建立会议日历。我的问题是如何在Javascript中添加或减去用户选择日期的时区。

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.

例如,在选择表单上,用户将从中选择日期形式:然后我会得到结果并转换为如下日期......

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

我想在此对象中添加12午夜。

I would like to add 12 midnight to this object.

然后我读入一个XML,它在两个城市之间的字符串中获得时区差异:例如喀布尔的+0430或纽约的-0400(夏令时)。这是基于GMT,。

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,.

然后我计算两个城市之间的时区差异:它将返回字符串830。 (我假设我必须将它作为日期对象返回?)。我完成了这部分返回一个字符串。我正在使用字符串。

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.

然后,我希望在一天中的24小时内完成循环,将喀布尔设置为午夜12点,然后循环播放。我很可能想到这一点 - 也就是说,在我循环时将日期设置为整个小时。

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.

我的问题是从喀布尔无痛地减去830看到会议时间将在纽约举行。

My problem is painlessly subtract the "830" from Kabul to see what the meeting time will be in New York.

如果我能从喀布尔时间中减去小时数和分钟数,那将是理想的选择。我注意到有人在javascript中减去了小时数,但没有减去分钟数。顺便说一下,那篇文章对我不起作用。

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.

我用没有分钟的字符串做了这个,但是我搞砸了会议记录。必须有一个更简单的方法。

I did this with strings without the minutes, but I mess up with the minutes. There has to be an easier way.

我会采用本机Javascript或jQuery的解决方案。

I would take a solution in either native Javascript or jQuery.

同样,我需要在特定日期减去/添加小时和分钟的时区差异。

Again, I need to subtract/add the time zone difference in hours and minutes to a certain date.

谢谢

推荐答案

date.setMinutes(date.getMinutes()+minutesToAdd);

上面的代码会将您的日期对象提前设置为minutesToAdd变量中的分钟数

The above code will set your date object ahead by the amount of minutes in the minutesToAdd variable

这篇关于使用javascript将日期和分钟添加到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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