Skip to content

Commit 05d90d7

Browse files
author
marker dao ®
committed
fix && feat(): Use isISOPartialDateString && Add tests
1 parent ff812f4 commit 05d90d7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

packages/devextreme/js/__internal/core/utils/m_date_serialization.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const NUMBER_SERIALIZATION_FORMAT = 'number';
77
const DATE_SERIALIZATION_FORMAT = 'yyyy/MM/dd';
88
const DATETIME_SERIALIZATION_FORMAT = 'yyyy/MM/dd HH:mm:ss';
99

10+
const ISO_PARTIAL_DATE_PATTERN = /^\d{4,}(-\d{2})?$/;
1011
const ISO8601_PATTERN = /^(\d{4,})(-)?(\d{2})(-)?(\d{2})(?:T(\d{2})(:)?(\d{2})?(:)?(\d{2}(?:\.(\d{1,3})\d*)?)?)?(Z|([+-])(\d{2})(:)?(\d{2})?)?$/;
1112
const ISO8601_TIME_PATTERN = /^(\d{2}):(\d{2})(:(\d{2}))?$/;
12-
1313
const ISO8601_PATTERN_PARTS = ['', 'yyyy', '', 'MM', '', 'dd', 'THH', '', 'mm', '', 'ss', '.SSS'];
1414
const DATE_SERIALIZATION_PATTERN = /^(\d{4})\/(\d{2})\/(\d{2})$/;
1515

@@ -35,8 +35,8 @@ function createLocalDateFromUTCTimestamp(timestamp: number): Date {
3535
return new Date(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate());
3636
}
3737

38-
function hasNoTimeOrTimezone(text: string): boolean {
39-
return isString(text) && !text.includes('T') && !text.includes(':');
38+
function isISOPartialDateString(text: string): boolean {
39+
return ISO_PARTIAL_DATE_PATTERN.test(text);
4040
}
4141

4242
function parseDate(text): string | Date {
@@ -63,7 +63,7 @@ function parseDate(text): string | Date {
6363
return text;
6464
}
6565

66-
return hasNoTimeOrTimezone(text)
66+
return isISOPartialDateString(text)
6767
? createLocalDateFromUTCTimestamp(parsedValue)
6868
: new Date(parsedValue);
6969
}
@@ -195,6 +195,7 @@ const getDateSerializationFormat = function (value) {
195195
};
196196

197197
const dateSerialization = {
198+
createLocalDateFromUTCTimestamp,
198199
dateParser,
199200
deserializeDate,
200201
serializeDate,

packages/devextreme/testing/tests/DevExpress.core/utils.date_serialization.tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ QUnit.test('deserializing first date (serialization format is string)', function
107107
assert.deepEqual(result, date, 'date is returned');
108108
});
109109

110+
QUnit.test('deserializing year-only string should return correct year (T1323373)', function(assert) {
111+
const result = dateSerialization.deserializeDate('2026');
112+
113+
assert.ok(result instanceof Date, 'result is a Date');
114+
assert.equal(result.getFullYear(), 2026, 'year is 2026');
115+
assert.equal(result.getMonth(), 0, 'month is January');
116+
assert.equal(result.getDate(), 1, 'day is 1');
117+
});
118+
119+
QUnit.test('createLocalDateFromUTCTimestamp returns local date with UTC components', function(assert) {
120+
const utcTimestamp = Date.UTC(2026, 0, 1);
121+
const result = dateSerialization.createLocalDateFromUTCTimestamp(utcTimestamp);
122+
123+
assert.ok(result instanceof Date, 'result is a Date');
124+
assert.equal(result.getFullYear(), 2026, 'year matches UTC year');
125+
assert.equal(result.getMonth(), 0, 'month matches UTC month');
126+
assert.equal(result.getDate(), 1, 'date matches UTC date');
127+
assert.equal(result.getHours(), 0, 'hours are 0');
128+
});
129+
110130
QUnit.test('serialization ISO8601 dates', function(assert) {
111131
const date = new Date(2015, 3, 5, 6, 7, 25, 125);
112132

0 commit comments

Comments
 (0)