Skip to content

Commit 5b891b8

Browse files
committed
MAGETWO-51629: Incorrect current time in date pickers
1 parent 7a04197 commit 5b891b8

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

app/code/Magento/Backend/view/adminhtml/templates/page/js/calendar.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ require([
5050
showTime: false,
5151
showHour: false,
5252
showMinute: false,
53+
serverTimezoneOffset: <?php echo (int) $block->getTimezoneOffsetSeconds(); ?>,
5354
yearRange: '<?php /* @escapeNotVerified */ echo $block->getYearRange() ?>'
5455
}
5556
});

lib/web/mage/calendar.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@
7373
var self = this;
7474

7575
/**
76-
* overwrite jQuery UI datepicker function.
76+
* Overwrite jQuery UI datepicker function.
77+
* Reason: magento date could be set before calendar show
78+
* but local date will be styled as current in original _generateHTML
79+
*
7780
* @param {Object} inst - instance datepicker.
7881
* @return {String} html template
7982
*/
8083
$.datepicker.constructor.prototype._generateHTML = function (inst) {
81-
var today = this._daylightSavingAdjust(self.getTimezoneDate()),
84+
var today = self.getTimezoneDate(),
8285
isRTL = this._get(inst, 'isRTL'),
8386
showButtonPanel = this._get(inst, 'showButtonPanel'),
8487
hideIfNoPrevNext = this._get(inst, 'hideIfNoPrevNext'),
@@ -313,17 +316,21 @@
313316

314317
/**
315318
* If server timezone is defined then take to account server timezone shift
316-
* @param {Object} date
317319
* @return {Object} date
318320
*/
319-
getTimezoneDate: function (date) {
320-
date = date || new Date();
321+
getTimezoneDate: function () {
321322

322-
if (this.options.serverTimezoneSeconds) {
323-
date.setTime((this.options.serverTimezoneSeconds + date.getTimezoneOffset() * 60) * 1000);
323+
// Get local time in ms
324+
var ms = Date.now();
325+
326+
// Adjust milliseconds according to store timezone offset
327+
// Mind the GMT zero offset
328+
if (typeof this.options.serverTimezoneOffset !== 'undefined') {
329+
// Make UTC time and add store timezone offset in seconds
330+
ms += (new Date().getTimezoneOffset() * 60 * 1000) + this.options.serverTimezoneOffset * 1000;
324331
}
325332

326-
return date;
333+
return new Date(ms);
327334
},
328335

329336
/**
@@ -515,9 +522,12 @@
515522
* @param {Object} el
516523
*/
517524
$.datepicker._gotoToday = function (el) {
518-
$.datepicker._gotoTodayOriginal.call(this, el);
519-
$.datepicker._selectDate.call(this, el);
520-
$(el).blur(); // To ensure that user can re-select date field without clicking outside it first.
525+
var pickerObject = $(el);
526+
527+
// Set date/time according to timezone offset
528+
pickerObject.datepicker( "setDate", pickerObject.calendar("getTimezoneDate") )
529+
// To ensure that user can re-select date field without clicking outside it first.
530+
.blur();
521531
};
522532

523533
return {

0 commit comments

Comments
 (0)