Skip to content

Commit cbbf0d4

Browse files
committed
fix(localize): work with dates of different timezones
1 parent ec8da8f commit cbbf0d4

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

packages/localize/src/formatDate.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,25 +206,25 @@ export function parseDate(date) {
206206
let parsedString;
207207
switch (memoizedGetDateFormatBasedOnLocale()) {
208208
case 'day-month-year':
209-
parsedString = `${stringToParse.slice(6, 10)}-${stringToParse.slice(
209+
parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice(
210210
3,
211211
5,
212-
)}-${stringToParse.slice(0, 2)}T00:00:00Z`;
212+
)}/${stringToParse.slice(0, 2)}`;
213213
break;
214214
case 'month-day-year':
215-
parsedString = `${stringToParse.slice(6, 10)}-${stringToParse.slice(
215+
parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice(
216216
0,
217217
2,
218-
)}-${stringToParse.slice(3, 5)}T00:00:00Z`;
218+
)}/${stringToParse.slice(3, 5)}`;
219219
break;
220220
case 'year-month-day':
221-
parsedString = `${stringToParse.slice(0, 4)}-${stringToParse.slice(
221+
parsedString = `${stringToParse.slice(0, 4)}/${stringToParse.slice(
222222
5,
223223
7,
224-
)}-${stringToParse.slice(8, 10)}T00:00:00Z`;
224+
)}/${stringToParse.slice(8, 10)}`;
225225
break;
226226
default:
227-
parsedString = '0000-00-00T00:00:00Z';
227+
parsedString = '0000/00/00';
228228
}
229229
const parsedDate = new Date(parsedString);
230230
// Check if parsedDate is not `Invalid Date`

packages/localize/test/LocalizeManager.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ describe('LocalizeManager', () => {
368368
date: 'I was written on {today, date}.',
369369
number: 'You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}',
370370
});
371-
const today = new Date('2018-04-30');
371+
const today = new Date('2018/04/30');
372372
expect(removeLtrRtl(manager.msg('my-ns:date', { today }))).to.equal(
373373
'I was written on 30 Apr 2018.',
374374
);

packages/localize/test/formatDate.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ beforeEach(() => {
1111

1212
describe('formatDate', () => {
1313
it('displays the appropriate date based on locale', async () => {
14-
const testDate = new Date('2012-05-21T00:00:00Z');
14+
const testDate = new Date('2012/05/21');
1515

1616
expect(formatDate(testDate)).to.equal('21/05/2012');
1717

@@ -29,7 +29,7 @@ describe('formatDate', () => {
2929
});
3030

3131
it('displays the date based on options', async () => {
32-
const testDate = new Date('2012-05-21T00:00:00Z');
32+
const testDate = new Date('2012/05/21');
3333
const options = {
3434
weekday: 'long',
3535
year: 'numeric',
@@ -120,31 +120,31 @@ describe('parseDate()', () => {
120120
Object.prototype.toString.call(value) === '[object Date]' && // is Date Object
121121
value.getDate() === date.getDate() && // day
122122
value.getMonth() === date.getMonth() && // month
123-
value.getFullYear() === date.getFullYear()
124-
); // year
123+
value.getFullYear() === date.getFullYear() // year
124+
);
125125
}
126126

127127
afterEach(() => {
128128
// makes sure that between tests the localization is reset to default state
129129
document.documentElement.lang = 'en-GB';
130130
});
131131
it('adds leading zeros', () => {
132-
expect(equalsDate(parseDate('1-1-1979'), new Date('1979-01-01'))).to.equal(true);
133-
expect(equalsDate(parseDate('1-11-1979'), new Date('1979-11-01'))).to.equal(true);
132+
expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true);
133+
expect(equalsDate(parseDate('1-11-1979'), new Date('1979/11/01'))).to.equal(true);
134134
});
135135
it('creates a date object', () => {
136136
expect(parseDate('10/10/2000') instanceof Date).to.equal(true);
137137
});
138138
it('returns a date object', () => {
139-
expect(equalsDate(parseDate('1-1-1979'), new Date('1979-01-01'))).to.equal(true);
140-
expect(equalsDate(parseDate('31.12.1970'), new Date('1970-12-31'))).to.equal(true);
139+
expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true);
140+
expect(equalsDate(parseDate('31.12.1970'), new Date('1970/12/31'))).to.equal(true);
141141
});
142142
it('handles all kind of delimiters', () => {
143-
expect(equalsDate(parseDate('12.12.1976'), new Date('1976-12-12'))).to.equal(true);
144-
expect(equalsDate(parseDate('13.12.1976'), new Date('1976-12-13'))).to.equal(true);
145-
expect(equalsDate(parseDate('14.12.1976'), new Date('1976-12-14'))).to.equal(true);
146-
expect(equalsDate(parseDate('14.12-1976'), new Date('1976-12-14'))).to.equal(true);
147-
expect(equalsDate(parseDate('14-12/1976'), new Date('1976-12-14'))).to.equal(true);
143+
expect(equalsDate(parseDate('12.12.1976'), new Date('1976/12/12'))).to.equal(true);
144+
expect(equalsDate(parseDate('13.12.1976'), new Date('1976/12/13'))).to.equal(true);
145+
expect(equalsDate(parseDate('14.12.1976'), new Date('1976/12/14'))).to.equal(true);
146+
expect(equalsDate(parseDate('14.12-1976'), new Date('1976/12/14'))).to.equal(true);
147+
expect(equalsDate(parseDate('14-12/1976'), new Date('1976/12/14'))).to.equal(true);
148148
});
149149
it('return undefined when no valid date provided', () => {
150150
expect(parseDate('12.12.1976.,')).to.equal(undefined);

0 commit comments

Comments
 (0)