3

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.

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

How would I convert this back to whole hours and minutes? I hate to sound ignorant, but I've been working on this for a week with little progress.
you can get the hours out using date.getHours() and the minutes using date.getMinutes().
So you could do var date = new Date(/*form data here*/); date.setMinutes(date.getMinutes() + calculateZoneDifference()); var timeString = date.getHours()+":"+date.getMinutes(); which would get you something that looks like 9:54
Glad to help :) don't forget to accept the answer so other people who end up using this page as reference in the future know what worked for you
Hmmm... I'm still having a problem, but it's with my timezone conversion code. In my code, if I subtract Kabul from New York, I get the necessary 8:30 time difference. But if I subtract New York from Kabul, I get -9:30 which is wrong (I should be getting -8:30). Can anyone direct me to a web site that determines how to calculate the time difference between two time zones based on the "-0400" (New York) and "+0430" (Kabul). I think I may have a solution if I sleep on it :-)
-1

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

date.setMinutes(date.getMinutes() - (hours*60+minutes))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.