开发者

Setting Javascript date object to midnight without being based on user computer date

I need to set a date and time to 12 midnight regardless of the user's computer date. I am creating an international meeting planner to offset time zones to get the meeting.

I have it working but I need to code now the differences in time zones. If I use new Date, it gives me the time based on the user's computer. For example, mine is Eastern US. If I try to do the time zone shift on November 6, 2011, Javascript/computer will calculate MY time zone shift at 2am. I do not want this.

My real goal is to set it to 12 midnight in the timezone that is where the meeting will be hosted from (let's say Afghanistan) and th开发者_开发知识库en calculate from there.

So:

  1. How do I set 12 midnight without being being the user's computer time?

  2. Can I set 12 midnight to a specific time zone, without being dependent on user's computer time?

I have to do this with Javascript as there is no server code involved.

Thanks


Something like this I would guess: new Date(Date.UTC(year, month, day, hour, minute, second))

w3schools Reference


To create midnight in GMT:

// Note: months are 0-based, so 7 == august
var midnight = new Date( Date.UTC(2011,7,15) );
// Sun Aug 14 2011 18:00:00 GMT-0600 (Mountain Daylight Time)

To create midnight in another time zone:

var mdt = -6; // Mountain Daylight Time
var midnightMDT = new Date( Date.UTC(2011,7,15,-mdt) );
//-> Mon Aug 15 2011 00:00:00 GMT-0600 (Mountain Daylight Time)

Date objects are expressed in the local time zone of the user, but they are still representative of midnight in another time zone.

If you want to express a date in another time zone, you'll need to offset the date to that timezone (setUTCHours()) and then use the various getUTC* methods (e.g. getUTCHours()) to construct your own string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜