fix(localize): work with dates of different timezones
This commit is contained in:
parent
ec8da8f12c
commit
cbbf0d49fb
3 changed files with 21 additions and 21 deletions
|
|
@ -206,25 +206,25 @@ export function parseDate(date) {
|
||||||
let parsedString;
|
let parsedString;
|
||||||
switch (memoizedGetDateFormatBasedOnLocale()) {
|
switch (memoizedGetDateFormatBasedOnLocale()) {
|
||||||
case 'day-month-year':
|
case 'day-month-year':
|
||||||
parsedString = `${stringToParse.slice(6, 10)}-${stringToParse.slice(
|
parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice(
|
||||||
3,
|
3,
|
||||||
5,
|
5,
|
||||||
)}-${stringToParse.slice(0, 2)}T00:00:00Z`;
|
)}/${stringToParse.slice(0, 2)}`;
|
||||||
break;
|
break;
|
||||||
case 'month-day-year':
|
case 'month-day-year':
|
||||||
parsedString = `${stringToParse.slice(6, 10)}-${stringToParse.slice(
|
parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice(
|
||||||
0,
|
0,
|
||||||
2,
|
2,
|
||||||
)}-${stringToParse.slice(3, 5)}T00:00:00Z`;
|
)}/${stringToParse.slice(3, 5)}`;
|
||||||
break;
|
break;
|
||||||
case 'year-month-day':
|
case 'year-month-day':
|
||||||
parsedString = `${stringToParse.slice(0, 4)}-${stringToParse.slice(
|
parsedString = `${stringToParse.slice(0, 4)}/${stringToParse.slice(
|
||||||
5,
|
5,
|
||||||
7,
|
7,
|
||||||
)}-${stringToParse.slice(8, 10)}T00:00:00Z`;
|
)}/${stringToParse.slice(8, 10)}`;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
parsedString = '0000-00-00T00:00:00Z';
|
parsedString = '0000/00/00';
|
||||||
}
|
}
|
||||||
const parsedDate = new Date(parsedString);
|
const parsedDate = new Date(parsedString);
|
||||||
// Check if parsedDate is not `Invalid Date`
|
// Check if parsedDate is not `Invalid Date`
|
||||||
|
|
|
||||||
|
|
@ -368,7 +368,7 @@ describe('LocalizeManager', () => {
|
||||||
date: 'I was written on {today, date}.',
|
date: 'I was written on {today, date}.',
|
||||||
number: 'You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}',
|
number: 'You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}',
|
||||||
});
|
});
|
||||||
const today = new Date('2018-04-30');
|
const today = new Date('2018/04/30');
|
||||||
expect(removeLtrRtl(manager.msg('my-ns:date', { today }))).to.equal(
|
expect(removeLtrRtl(manager.msg('my-ns:date', { today }))).to.equal(
|
||||||
'I was written on 30 Apr 2018.',
|
'I was written on 30 Apr 2018.',
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ beforeEach(() => {
|
||||||
|
|
||||||
describe('formatDate', () => {
|
describe('formatDate', () => {
|
||||||
it('displays the appropriate date based on locale', async () => {
|
it('displays the appropriate date based on locale', async () => {
|
||||||
const testDate = new Date('2012-05-21T00:00:00Z');
|
const testDate = new Date('2012/05/21');
|
||||||
|
|
||||||
expect(formatDate(testDate)).to.equal('21/05/2012');
|
expect(formatDate(testDate)).to.equal('21/05/2012');
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ describe('formatDate', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('displays the date based on options', async () => {
|
it('displays the date based on options', async () => {
|
||||||
const testDate = new Date('2012-05-21T00:00:00Z');
|
const testDate = new Date('2012/05/21');
|
||||||
const options = {
|
const options = {
|
||||||
weekday: 'long',
|
weekday: 'long',
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
|
|
@ -120,8 +120,8 @@ describe('parseDate()', () => {
|
||||||
Object.prototype.toString.call(value) === '[object Date]' && // is Date Object
|
Object.prototype.toString.call(value) === '[object Date]' && // is Date Object
|
||||||
value.getDate() === date.getDate() && // day
|
value.getDate() === date.getDate() && // day
|
||||||
value.getMonth() === date.getMonth() && // month
|
value.getMonth() === date.getMonth() && // month
|
||||||
value.getFullYear() === date.getFullYear()
|
value.getFullYear() === date.getFullYear() // year
|
||||||
); // year
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|
@ -129,22 +129,22 @@ describe('parseDate()', () => {
|
||||||
document.documentElement.lang = 'en-GB';
|
document.documentElement.lang = 'en-GB';
|
||||||
});
|
});
|
||||||
it('adds leading zeros', () => {
|
it('adds leading zeros', () => {
|
||||||
expect(equalsDate(parseDate('1-1-1979'), new Date('1979-01-01'))).to.equal(true);
|
expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true);
|
||||||
expect(equalsDate(parseDate('1-11-1979'), new Date('1979-11-01'))).to.equal(true);
|
expect(equalsDate(parseDate('1-11-1979'), new Date('1979/11/01'))).to.equal(true);
|
||||||
});
|
});
|
||||||
it('creates a date object', () => {
|
it('creates a date object', () => {
|
||||||
expect(parseDate('10/10/2000') instanceof Date).to.equal(true);
|
expect(parseDate('10/10/2000') instanceof Date).to.equal(true);
|
||||||
});
|
});
|
||||||
it('returns a date object', () => {
|
it('returns a date object', () => {
|
||||||
expect(equalsDate(parseDate('1-1-1979'), new Date('1979-01-01'))).to.equal(true);
|
expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true);
|
||||||
expect(equalsDate(parseDate('31.12.1970'), new Date('1970-12-31'))).to.equal(true);
|
expect(equalsDate(parseDate('31.12.1970'), new Date('1970/12/31'))).to.equal(true);
|
||||||
});
|
});
|
||||||
it('handles all kind of delimiters', () => {
|
it('handles all kind of delimiters', () => {
|
||||||
expect(equalsDate(parseDate('12.12.1976'), new Date('1976-12-12'))).to.equal(true);
|
expect(equalsDate(parseDate('12.12.1976'), new Date('1976/12/12'))).to.equal(true);
|
||||||
expect(equalsDate(parseDate('13.12.1976'), new Date('1976-12-13'))).to.equal(true);
|
expect(equalsDate(parseDate('13.12.1976'), new Date('1976/12/13'))).to.equal(true);
|
||||||
expect(equalsDate(parseDate('14.12.1976'), new Date('1976-12-14'))).to.equal(true);
|
expect(equalsDate(parseDate('14.12.1976'), new Date('1976/12/14'))).to.equal(true);
|
||||||
expect(equalsDate(parseDate('14.12-1976'), new Date('1976-12-14'))).to.equal(true);
|
expect(equalsDate(parseDate('14.12-1976'), new Date('1976/12/14'))).to.equal(true);
|
||||||
expect(equalsDate(parseDate('14-12/1976'), new Date('1976-12-14'))).to.equal(true);
|
expect(equalsDate(parseDate('14-12/1976'), new Date('1976/12/14'))).to.equal(true);
|
||||||
});
|
});
|
||||||
it('return undefined when no valid date provided', () => {
|
it('return undefined when no valid date provided', () => {
|
||||||
expect(parseDate('12.12.1976.,')).to.equal(undefined);
|
expect(parseDate('12.12.1976.,')).to.equal(undefined);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue