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!

12
  • While possibly not a duplicate, the answer can be found here: stackoverflow.com/questions/439630/… with another related (same answer) stackoverflow.com/questions/32214577/…
    – fdomn-m
    Aug 26, 2015 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, 2015 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?
    – fdomn-m
    Aug 26, 2015 at 10:54
  • I tried the but the date has to be sent as a Date object to pass validation..
    – Celt
    Aug 26, 2015 at 10:56
  • 1
    Same question as earlier, different answer: 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.
    – fdomn-m
    Aug 26, 2015 at 11:07

0

Your Answer

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

Browse other questions tagged or ask your own question.