0

I have a script that stores an action taken by a user. There's a column that contains datetime and originally I user NOW(), but that uses server time, which is a few hours off as compared to the user's actual time.

So I decided I'll use the time that I can get with JS. I've formatted it this way:

var now   = new Date(),                
    isnow = now.getFullYear() + '-' + ('0' + (now.getMonth() + 1)).slice(-2) + '-' + ('0' + now.getDate()).slice(-2) + ' ' + ('0' + (now.getHours() + 1)).slice(-2) + ':' + ('0' + now.getMinutes()).slice(-2) + ':' + ('0' + now.getSeconds()).slice(-2);

I've tested and while the format works fine, the time is off by an hour. Is it because of the Daylight Savings Time? How do I get the actual local time for the user?

santa
  • 12,234
  • 49
  • 155
  • 255
  • take a look at here. [http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours](http://stackoverflow.com/questions/11887934/check-if-daylight-saving-time-is-in-effect-and-if-it-is-for-how-many-hours) – Saehun Sean Oh Sep 17 '15 at 21:02
  • 1
    You should use UTC so that the time is independent of the user's systems settings (assuming the clock is accurate). That removes any issues with timezones, however the system clock may not be set to the correct time. – RobG Sep 17 '15 at 21:06
  • Is there a reason you're using `now.getHours() + 1` instead of just `now.getHours()`? – Paul Roub Sep 17 '15 at 21:08
  • Agree with @RobG ... I do all time in GMT/UTC then client-js UTC time _is the same as_ server-sql `CURRENT_TIMESTAMP` UTC time, even though the client and server clocks show different local times. ("CURRENT_TIMESTAMP" is the ANSI way, while NOW() is mysql specific). Then your client javascript can take the UTC time it gets from the server and display it in local time. – Stephen P Sep 17 '15 at 22:06

2 Answers2

2

In your code wrote:

...('0' + (now.getHours() + 1)).slice(-2)...

Try to remove this plus one

Additional you can check if Day Savings Time with:

if (now.dst()) { alert ("Daylight savings time!"); }

Date.prototype.stdTimezoneOffset = function() {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.dst = function() {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

Based at answer similar issue

Community
  • 1
  • 1
0

You should use the toISOString() method to convert the Date object to the ISO-8601 standard format:

now.toISOString();

The ISO-8601 date format puts the time information into a universal form which includes optional timezone information (likely the source of your issues).

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • I would have to format it further, like replacing "T" with space and stripping trailing chars after seconds. – santa Sep 17 '15 at 21:04
  • @santa - Changing spacing would be trivial, the important part of using ISO-8601 is that it's converted to UTC offset so you don't need to worry about timezone information (which is the current source of your problems). If you'd like to modify how `toISOSTring()` functions, you can modify the output directly or modify the "polyfill" option provided here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString – Mr. Llama Sep 17 '15 at 21:09
  • But if I don't know what the timezone for user it could be a problem. Also I could store GMT time in my mysql directly. – santa Sep 17 '15 at 21:15