Skip to content

Commit

Permalink
Add comment explaining moment to Date conversion
Browse files Browse the repository at this point in the history
Fixes #5478
  • Loading branch information
jbudz committed Dec 8, 2015
1 parent 7c753a3 commit e61a176
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/ui/public/timepicker/offset_timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ define(function (require) {
var angular = require('angular');
var moment = require('moment');

/**
* moment objects can have an associated timezone, and when converting to a Date the
* timezone is changed to browser time. This can cause issues, such as a day picker
* showing the wrong day.
* When a moment date is passed in, offset the timezone so that after converting to a Date object
* the day does not appear changed. When reading back, convert to moment and remove the offset.
*/
require('ui/modules')
.get('kibana')
.directive('offsetTimezone', function () {
Expand All @@ -18,6 +25,7 @@ define(function (require) {
// continue to offset the date.
var offsetDate = false;

//Going from Date object to moment
function fromDatePicker(value) {
if (!value) return;
var date = moment(value);
Expand All @@ -30,10 +38,11 @@ define(function (require) {
return date;
}

//Going from moment to Date object
function toDatePicker(value) {
if (!value) return;

var date = new Date(value.format('YYYY-MM-DDTHH:mm:ss.SSSZ'));

var offset = date.getTimezoneOffset() + value.utcOffset();
date.setMinutes(date.getMinutes() + offset);
offsetDate = true;
Expand Down

0 comments on commit e61a176

Please sign in to comment.