diff --git a/app/code/Magento/Ui/view/base/web/js/form/element/date.js b/app/code/Magento/Ui/view/base/web/js/form/element/date.js index 202cfe93069b0..eb8dfb4b4057e 100644 --- a/app/code/Magento/Ui/view/base/web/js/form/element/date.js +++ b/app/code/Magento/Ui/view/base/web/js/form/element/date.js @@ -53,13 +53,12 @@ define([ pickerDefaultDateFormat: 'MM/dd/y', // ICU Date Format pickerDefaultTimeFormat: 'h:mm a', // ICU Time Format + elementTmpl: 'ui/form/element/date', + /** - * Moment compatible format used for moment conversion - * of date from datePicker + * Format needed by moment timezone for conversion */ - momentFormat: 'MM/DD/YYYY', - - elementTmpl: 'ui/form/element/date', + timezoneFormat: 'YYYY-MM-DD HH:mm', listens: { 'value': 'onValueChange', @@ -141,15 +140,17 @@ define([ */ onShiftedValueChange: function (shiftedValue) { var value, - formattedValue; + formattedValue, + momentValue; if (shiftedValue) { + momentValue = moment(shiftedValue, this.pickerDateTimeFormat); + if (this.options.showsTime) { - formattedValue = moment(shiftedValue).format('YYYY-MM-DD HH:mm'); + formattedValue = moment(momentValue).format(this.timezoneFormat); value = moment.tz(formattedValue, this.storeTimeZone).tz('UTC').toISOString(); } else { - value = moment(shiftedValue, this.momentFormat); - value = value.format(this.outputDateFormat); + value = momentValue.format(this.outputDateFormat); } } else { value = ''; @@ -166,8 +167,6 @@ define([ */ prepareDateTimeFormats: function () { this.pickerDateTimeFormat = this.options.dateFormat; - this.momentFormat = this.options.dateFormat ? - utils.convertToMomentFormat(this.options.dateFormat) : this.momentFormat; if (this.options.showsTime) { this.pickerDateTimeFormat += ' ' + this.options.timeFormat; diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/date-time.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/date-time.test.js new file mode 100644 index 0000000000000..4271ecc563b9a --- /dev/null +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Ui/base/js/form/element/date-time.test.js @@ -0,0 +1,41 @@ +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/*eslint max-nested-callbacks: 0*/ + +define([ + 'Magento_Ui/js/form/element/date', + 'mageUtils', + 'moment' +], function (DateElement, utils, moment) { + 'use strict'; + + describe('Magento_Ui/js/form/element/date', function () { + var params, model; + + beforeEach(function () { + params = { + dataScope: 'abstract', + options: { + showsTime: true + } + }; + model = new DateElement(params); + }); + + it('Check prepareDateTimeFormats function', function () { + spyOn(utils, 'convertToMomentFormat').and.callThrough(); + model.prepareDateTimeFormats(); + expect(utils.convertToMomentFormat).toHaveBeenCalled(); + }); + + it('Check onShiftedValueChange function', function () { + spyOn(moment, 'tz').and.callThrough(); + model.onShiftedValueChange('2016-12-23 9:11 PM'); + expect(moment.tz).toHaveBeenCalled(); + }); + + }); +}); diff --git a/dev/tests/js/jasmine/tests/lib/mage/misc.test.js b/dev/tests/js/jasmine/tests/lib/mage/misc.test.js index a9ea5ed90192f..62950372d3a73 100644 --- a/dev/tests/js/jasmine/tests/lib/mage/misc.test.js +++ b/dev/tests/js/jasmine/tests/lib/mage/misc.test.js @@ -15,7 +15,7 @@ define([ var format, momentFormat; format = 'M/d/yy'; - momentFormat = 'MM/DD/YYYY'; + momentFormat = 'M/DD/YYYY'; expect(utils.convertToMomentFormat(format)).toBe(momentFormat); }); diff --git a/lib/web/mage/utils/misc.js b/lib/web/mage/utils/misc.js index 29fed485f290c..3fdcf3e7de542 100644 --- a/lib/web/mage/utils/misc.js +++ b/lib/web/mage/utils/misc.js @@ -220,7 +220,6 @@ define([ newFormat = format.replace(/yy|y/gi, 'YYYY'); // replace the year newFormat = newFormat.replace(/dd|d/g, 'DD'); // replace the date - newFormat = newFormat.replace(/mm|m/gi, 'MM'); //replace the month return newFormat; }