最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Adding hours and minutes to a date with JavaScript - Stack Overflow

programmeradmin5浏览0评论

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

I would like to add 12 midnight to this object.

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

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.

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.

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

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.

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.

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

I would like to add 12 midnight to this object.

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

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.

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.

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

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.

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.

Share Improve this question edited Dec 21, 2019 at 23:55 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Jul 18, 2011 at 14:20 user581733user581733 5043 gold badges9 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12
date.setMinutes(date.getMinutes()+minutesToAdd);

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

Easiest would be to calculate the minutes for that time delta then do a minutes delta.

date.setMinutes(date.getMinutes() - (hours*60+minutes))
发布评论

评论列表(0)

  1. 暂无评论