1

I have a problem here. I need to change the timezone of of a date in Javascript to UTC before passing it to the back end pf my service for validation. I cannot find a solution in any of the questions on this site or on other sites. The problem is that every method I have tried so far is also changing the time and date of my Javascript date object. For example:

var startDate = new Date($("#start-date-picker").val()).toUTCString();

Changes the time which affects the date (the timezone of my laptop is currently set to GMT +2).

While debugging my date object looks like this:

var startDate = new Date(getProperDate($("#start-date-picker").val())); 
//startDate = Wed Aug 26 2015 00:00:00 GMT+0200 (Romance Daylight Time) {}

But when changed using the .toUTCString() method the date ends up like this:

startDate2 = "Tue, 25 Aug 2015 22:00:00 GMT"

I cannot find a way to change just the timezone and preserve the current date and time. I cannot use any external libraries either before anyone suggests moment.js or any others. Any help would be much appreciated, thanks!

Celt
  • 2,469
  • 2
  • 26
  • 43
  • While possibly not a duplicate, the answer can be found here: http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s/16048201#16048201 with another related (same answer) http://stackoverflow.com/questions/32214577/javascript-new-date-changing-hour-value/32215100#comment52329943_32215100 – freedomn-m Aug 26 '15 at 10:49
  • No I don't want the date or time to change just the timezone, these answers will change them. – Celt Aug 26 '15 at 10:50
  • That makes no sense. The timezone is an integral part of the time. `toUTCString` converts to GMT based on the current timezone (so if it's midnight GMT+2 then it's 10pm GMT). How are you "passing it to the back end"? Could you just use a custom date format `toString` that doesn't include the timezone? – freedomn-m Aug 26 '15 at 10:54
  • I tried the but the date has to be sent as a Date object to pass validation.. – Celt Aug 26 '15 at 10:56
  • 1
    Same question as earlier, different answer: http://stackoverflow.com/a/14006555/2181514. You need to *create* the date as a UTC date using createDateAsUTC instead of letting js interpret the timezone. `toUTCString` just says "output this date/time with the equivalent GMT time" - which isn't what you want. – freedomn-m Aug 26 '15 at 11:07
  • Maybe include the code you use to pass to the back end? – freedomn-m Aug 26 '15 at 11:09
  • Thanks I'll give that a go first and if it doesn't work I'll post the rest of my code. – Celt Aug 26 '15 at 11:12
  • The method in that question doesn't set the timezone either, I think I have a solution though will update if it works. – Celt Aug 26 '15 at 11:18
  • No - it creates a date with UTC timezone. – freedomn-m Aug 26 '15 at 11:28
  • Nope when I done it like this `var startDateAsUtc = new Date(Date.UTC(startDate.getUTCFullYear(), startDate.getUTCMonth(), startDate.getUTCDay(), 0, 0, 0));` I got this result: `startDateAsUtc = Sun Aug 02 2015 02:00:00 GMT+0200 (Romance Daylight Time) {}` – Celt Aug 26 '15 at 11:52
  • Yes - that's correct. You've create a UTC date then *displayed* it as GMT+2. If you're passing the `Date` object, then there's no need to display it and your service will get the midnight version. – freedomn-m Aug 26 '15 at 12:05
  • Wait my bad I was using `getUTCDay()` instead of `getUTCDate()` when setting the day of the month and I was getting the wrong date back which was throwing me off. Thanks for your help! – Celt Aug 26 '15 at 12:13

0 Answers0