-1

consider am having the string date and time. say

var selfwDepDate    = document.forms[0].fwDepartureDate.value;
var selfwDepTime    = document.forms[0].fwDepartureTime.value;

and having time zone as

var orgTimeZone = document.getElementById('orginTimeZone').value;
var descTimeZone = document.getElementById('descTimeZone').value;

can any one how to convert the String date to GMT date.. value of orgTimeZone = Asia/Dubai value of descTimeZone = America/New_York

A Programmer
  • 368
  • 4
  • 7
  • 26
  • Please check this question http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript – Guruparan Apr 16 '12 at 06:06
  • None of these helped? [convert one timezone to another](http://stackoverflow.com/search?q=convert+one+timezone+to+another+%5Djavascript%5B) – mplungjan Apr 16 '12 at 06:07
  • 1
    @Guruparan - not a very clear question to answer this one with - I would think >[this one](http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript)< more appropriate – mplungjan Apr 16 '12 at 06:11
  • hi thankz for the reply. guyz i need to convert the date to GMT i mean UTC,, so any one tell me, wats the reference time zone to convert that...var dt = new timezoneJS.Date(selfwDepDate,orgTimeZone ); dt.setTimezone(?????); alert('final date'+ dt); – A Programmer Apr 16 '12 at 06:14

1 Answers1

2

I think you can use timezone-js.

Create a timezoneJS.Date the same way as a normal JavaScript Date, but append a timezone parameter on the end:

var dt = new timezoneJS.Date('10/31/2008',
  'America/New_York');
var dt = new timezoneJS.Date(2008, 9, 31, 11, 45,
  'America/Los_Angeles');

OR

you can approach like this:-

var now = new Date();

var utc = new Date(Date.UTC(
    now.getFullYear(),
    now.getMonth(),
    now.getDate(),
    now.getHours(),
    now.getMinutes()
));

Now for different timezone you need to add or subtract the time by which they differ from GMT.
Note :-Date.UTC() returns the number of milliseconds since the epoch, UTC.

Pranav
  • 8,563
  • 4
  • 26
  • 42
  • hey i got a page error fot var dt = new timezoneJS.Date('10/31/2008', 'America/New_York'); since i passed date as 16-Apr-2012 06:30 – A Programmer Apr 16 '12 at 06:23
  • any way to resolve this page error.. since my date format is dynamic – A Programmer Apr 16 '12 at 06:29
  • 1
    take the date from user but parse it in a proper format . you can search on SO, for lot of answers about how to parse a date format – Pranav Apr 16 '12 at 06:33
  • even var dt = new timezoneJS.Date('10/31/2008', 'America/New_York'); alert('final date'+ dt); its not working.. do we need to have any jar for this – A Programmer Apr 16 '12 at 06:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10110/discussion-between-meeran-and-pranav) – A Programmer Apr 16 '12 at 06:42
  • 1
    have u downloaded the src file and attached it in your script tag:- https://github.com/mde/timezone-js – Pranav Apr 16 '12 at 06:42