fix(localize): work with dates of different timezones

This commit is contained in:
Thomas Allmer 2019-04-26 13:36:57 +02:00
parent ec8da8f12c
commit cbbf0d49fb
3 changed files with 21 additions and 21 deletions

View file

@ -206,25 +206,25 @@ export function parseDate(date) {
let parsedString;
switch (memoizedGetDateFormatBasedOnLocale()) {
case 'day-month-year':
parsedString = `${stringToParse.slice(6, 10)}-${stringToParse.slice(
parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice(
3,
5,
)}-${stringToParse.slice(0, 2)}T00:00:00Z`;
)}/${stringToParse.slice(0, 2)}`;
break;
case 'month-day-year':
parsedString = `${stringToParse.slice(6, 10)}-${stringToParse.slice(
parsedString = `${stringToParse.slice(6, 10)}/${stringToParse.slice(
0,
2,
)}-${stringToParse.slice(3, 5)}T00:00:00Z`;
)}/${stringToParse.slice(3, 5)}`;
break;
case 'year-month-day':
parsedString = `${stringToParse.slice(0, 4)}-${stringToParse.slice(
parsedString = `${stringToParse.slice(0, 4)}/${stringToParse.slice(
5,
7,
)}-${stringToParse.slice(8, 10)}T00:00:00Z`;
)}/${stringToParse.slice(8, 10)}`;
break;
default:
parsedString = '0000-00-00T00:00:00Z';
parsedString = '0000/00/00';
}
const parsedDate = new Date(parsedString);
// Check if parsedDate is not `Invalid Date`

View file

@ -368,7 +368,7 @@ describe('LocalizeManager', () => {
date: 'I was written on {today, date}.',
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(
'I was written on 30 Apr 2018.',
);

View file

@ -11,7 +11,7 @@ beforeEach(() => {
describe('formatDate', () => {
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');
@ -29,7 +29,7 @@ describe('formatDate', () => {
});
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 = {
weekday: 'long',
year: 'numeric',
@ -120,8 +120,8 @@ describe('parseDate()', () => {
Object.prototype.toString.call(value) === '[object Date]' && // is Date Object
value.getDate() === date.getDate() && // day
value.getMonth() === date.getMonth() && // month
value.getFullYear() === date.getFullYear()
); // year
value.getFullYear() === date.getFullYear() // year
);
}
afterEach(() => {
@ -129,22 +129,22 @@ describe('parseDate()', () => {
document.documentElement.lang = 'en-GB';
});
it('adds leading zeros', () => {
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-1-1979'), new Date('1979/01/01'))).to.equal(true);
expect(equalsDate(parseDate('1-11-1979'), new Date('1979/11/01'))).to.equal(true);
});
it('creates a date object', () => {
expect(parseDate('10/10/2000') instanceof Date).to.equal(true);
});
it('returns a date object', () => {
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('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);
});
it('handles all kind of delimiters', () => {
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('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('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('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', () => {
expect(parseDate('12.12.1976.,')).to.equal(undefined);