3

I have a string, and I have converted it into a Date variable. But the timezone is turning out to be wrong.

The string I'm trying to use is :

var v = "2013/09/05 17:53 -05:00";
var parsedvalueInField = new Date( v );

If I do an alert of parsedvalueInField,the output I get is:

 Thu Sep 05 2013 18:53:00 GMT-0400 ( Eastern Daylight Time);

How do I go about rectifying this difference in Timezone?

Please Help!

stackunderflow
  • 953
  • 7
  • 17
user2583714
  • 1,097
  • 2
  • 11
  • 18

1 Answers1

2

2013/09/05 17:53 -05:00 is the same time as Thu Sep 05 2013 18:53:00 GMT-0400; both are Thu, 05 Sep 2013 22:53:00 GMT

In JavaScript, you have two choices (natively) about how to display a time; in the local machine's timezone (Date.prototype.toString) or in UTC (Date.prototype.toUTCString). If you want to display a time as a string with a different time zone, you will have to write a function to do it manually, calculating it from UTC.

The two main articles on MDN which will help you with how to use a Date are Date and Date.prototype.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • For more info on UTC: http://www.w3schools.com/jsref/jsref_utc.asp, also check out this post http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s – Barrett Sep 05 '13 at 18:29
  • 6
    @Barrett please don't use w3schools as a primary reference; it is entirely seperate to the w3c and is not a wiki. http://www.w3fools.com/ . Use places like MDN or official w3 documents. – Paul S. Sep 05 '13 at 18:30
  • 5
    sorry i didn't know, MDN UTC https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC – Barrett Sep 05 '13 at 18:36