0

I have a JavaScript function that compares 2 dates. I am trying to format the dates properly but the Date is subtracting 4 hours (presumably to compensate for GMT and EST), unnecessarily.

$.each(json.data, function(i, v) {              
        $.each(zoneObj, function(k, z) {  
            if (v.ptid == z.ptid) { 
                console.log(v.timeStamp);
                var d = new Date(v.timeStamp);
                console.log("datTime " + d);

Result enter image description here

I see that Date is converting the v.timeStamp to Easter Standard but that isn't necessary. How do I disable this?

I need to use Date in order to take advantage of the getMonth, getMinute, etc. methods

PT_C
  • 1,178
  • 5
  • 24
  • 57
  • Can you add bit more code around it, including very basic html? Maybe use some like jsfiddle.net so it's easier to help. – sal Jun 07 '16 at 17:19
  • 1
    Ah, timezones. So, it's not **subtracting** 4 hours. It's the same moment in time, just different hours of the day depending which timezone. How are you using this date? Do you want to keep it in the client's timezone (EST)? – Alex Johnson Jun 07 '16 at 17:19
  • 1
    Possible duplicate of [Getting the client's timezone in JavaScript](http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript) – devlin carnate Jun 07 '16 at 17:21
  • Please don't rely on images, not everyone can see them. Post the equivalent as text. – RobG Jun 07 '16 at 23:59

2 Answers2

2

Most of the methods for the Date object will use the local time which in your case is Eastern Standard Time. If you want to use UTC, you need to specify those methods explicitly.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Schleis
  • 41,516
  • 7
  • 68
  • 87
1

You can get UTC values of the standard JS Date object.

The functions are: getUTCMilliseconds(), getUTCSeconds(), getUTCMinutes(), getUTCHours(), getUTCFullYear(), getUTCDay() and getUTCDate()

Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

But I would suggest you to use moment.js, which is far more powerful than standard JS Date and it is used by everyone.

Read more here: http://momentjs.com

Andras Szell
  • 527
  • 2
  • 13
  • Please do not reference w3schools, the site is full of errors. Reference either [*ECMA-262*](http://www.ecma-international.org/ecma-262/6.0/#sec-date-constructor) or [*MDN*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). – RobG Jun 07 '16 at 23:59