Skip to content

Commit 8320272

Browse files
Merge pull request #46 from DevExpress/format_in_utc
Format date in UTC timezone
2 parents cba32f7 + 4c2de41 commit 8320272

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/date.js

Lines changed: 21 additions & 7 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 '';
@@ -175,6 +188,7 @@ dateLocalization.inject({
175188
format = format.type || format;
176189

177190
var intlFormat = getIntlFormat(format);
191+
178192
if(intlFormat) {
179193
return getIntlFormatter(intlFormat)(date);
180194
}
@@ -274,9 +288,9 @@ dateLocalization.inject({
274288
},
275289

276290
getFormatParts: function(format) {
277-
var utcFormat = objectAssign({}, intlFormats[format.toLowerCase()], { timeZone: 'UTC' });
278-
var utcDate = new Date(Date.UTC(2001, 2, 4, 5, 6, 7));
279-
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);
280294

281295
formattedDate = normalizeNumerals(formattedDate);
282296

0 commit comments

Comments
 (0)