Skip to content

Commit 4c2de41

Browse files
Move all UTC timezone usage to one method
1 parent 48a6a3b commit 4c2de41

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/date.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,23 @@ var SYMBOLS_TO_REMOVE_REGEX = /[\u200E\u200F]/g;
88

99
var getIntlFormatter = function(format) {
1010
return function(date) {
11-
return (new Intl.DateTimeFormat(locale(), format)).format(date).replace(SYMBOLS_TO_REMOVE_REGEX, '');
11+
// Intl in some browsers formates dates with timezone offset which was at the moment for this date.
12+
// But the method "new Date" creates date using current offset. So, we decided to format dates in the UTC timezone.
13+
if(!format.timeZoneName) {
14+
var utcDate = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()),
15+
utcFormat = objectAssign({ timeZone: 'UTC' }, format);
16+
17+
return formatDateTime(utcDate, utcFormat);
18+
}
19+
20+
return formatDateTime(date, format);
1221
};
1322
};
1423

24+
var formatDateTime = function(date, format) {
25+
return (new Intl.DateTimeFormat(locale(), format)).format(date).replace(SYMBOLS_TO_REMOVE_REGEX, '');
26+
};
27+
1528
var formatNumber = function(number) {
1629
return (new Intl.NumberFormat(locale())).format(number);
1730
};
@@ -137,7 +150,7 @@ dateLocalization.inject({
137150

138151
var getIntlDayNames = function(format) {
139152
return Array.apply(null, new Array(7)).map(function(_, dayIndex) {
140-
return getIntlFormatter({ weekday: format, timeZone: 'UTC' })(new Date(Date.UTC(0, 0, dayIndex)));
153+
return getIntlFormatter({ weekday: format })(new Date(0, 0, dayIndex));
141154
});
142155
};
143156

@@ -147,11 +160,11 @@ dateLocalization.inject({
147160
},
148161

149162
getPeriodNames: function() {
150-
var hour12Formatter = getIntlFormatter({ hour: 'numeric', hour12: true, timeZone: 'UTC' });
163+
var hour12Formatter = getIntlFormatter({ hour: 'numeric', hour12: true });
151164

152165
return [ 1, 13 ].map(function(hours) {
153166
var hourNumberText = formatNumber(1); // NOTE: For 'bn' locale
154-
var timeParts = hour12Formatter(new Date(Date.UTC(0, 0, 1, hours))).split(hourNumberText);
167+
var timeParts = hour12Formatter(new Date(0, 0, 1, hours)).split(hourNumberText);
155168

156169
if(timeParts.length !== 2) {
157170
return '';
@@ -176,27 +189,16 @@ dateLocalization.inject({
176189

177190
var intlFormat = getIntlFormat(format);
178191

179-
// There are differences between date formatting in different browsers. All browsers create date by 'new Date()'
180-
// with current timezone offset for all date. Chrome and Firefox format this date with an offset for the created date,
181-
// but IE and Edge use the current offset.
182-
var utcDate = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
183-
184192
if(intlFormat) {
185-
intlFormat.timeZone = 'UTC';
186-
return getIntlFormatter(intlFormat)(utcDate);
193+
return getIntlFormatter(intlFormat)(date);
187194
}
188195

189196
var formatType = typeof format;
190197
if(format.formatter || formatType === 'function' || formatType === 'string') {
191198
return this.callBase.apply(this, arguments);
192199
}
193200

194-
if(format.timeZoneName) {
195-
return getIntlFormatter(format)(date);
196-
} else {
197-
format.timeZone = 'UTC';
198-
return getIntlFormatter(format)(utcDate);
199-
}
201+
return getIntlFormatter(format)(date);
200202
},
201203

202204
parse: function(dateString, format) {
@@ -286,9 +288,9 @@ dateLocalization.inject({
286288
},
287289

288290
getFormatParts: function(format) {
289-
var utcFormat = objectAssign({}, intlFormats[format.toLowerCase()], { timeZone: 'UTC' });
290-
var utcDate = new Date(Date.UTC(2001, 2, 4, 5, 6, 7));
291-
var formattedDate = getIntlFormatter(utcFormat)(utcDate);
291+
var intlFormat = objectAssign({}, intlFormats[format.toLowerCase()]);
292+
var date = new Date(2001, 2, 4, 5, 6, 7);
293+
var formattedDate = getIntlFormatter(intlFormat)(date);
292294

293295
formattedDate = normalizeNumerals(formattedDate);
294296

0 commit comments

Comments
 (0)