Created
February 2, 2020 00:30
-
-
Save bradyclifford/54317cabf8ed98333f8086f44262b99f to your computer and use it in GitHub Desktop.
JavaScript Date Assessment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Assessment: | |
* Native date creation -> Non-ISO will create date in current timezone | |
* Native date creation -> ISO will create date in GMT timezone | |
- When parsing "literal" ISO, parseISO & parseJSON are same - identical to native | |
* When parsing "short" ISO, parseISO converts ISO string yyyy/mm/dd to local timezone (adds time offset) unlike native | |
* formatISO should be used for converstion to date string yyyy/mm/dd? What happens when it is converted back into a DateTime? Assume UTC? | |
* DatePicker, push in a parseISO date or ISO string, outputs a date type with HST offset | |
* | |
*/ | |
describe('Date: Timezones', () => { | |
// December 31, 2020 23:59:59 HST | Janurary , 2021 09:59:59 GMT | 2021-01-01T09:59:59.999Z | |
systemDate = new Date(1609495199999); | |
it('show native date creation with timezones into UTC standard', () => { | |
// 2021-01-01T09:59:59.999Z - explict | |
expect(new Date(global.Date.now()).valueOf()).toBe(1609495199999); | |
expect(new Date(global.Date.now()).toISOString()).toBe('2021-01-01T09:59:59.999Z'); // UTC | |
expect(new Date(global.Date.now()).toUTCString()).toBe('Fri, 01 Jan 2021 09:59:59 GMT'); | |
expect(new Date(global.Date.now()).toLocaleString()).toBe('12/31/2020, 11:59:59 PM'); | |
expect(new Date(global.Date.now()).getDate()).toBe(31); // HST Day | |
expect(new Date(global.Date.now()).getUTCDate()).toBe(1); // GMT Day | |
// ISO "Literal" String | |
expect(new Date('2021-01-01T09:59:59.999Z').valueOf()).toBe(1609495199999); | |
expect(new Date('2021-01-01T09:59:59.999Z').toISOString()).toBe('2021-01-01T09:59:59.999Z'); // UTC | |
expect(new Date('2021-01-01T09:59:59.999Z').toUTCString()).toBe('Fri, 01 Jan 2021 09:59:59 GMT'); | |
expect(new Date('2021-01-01T09:59:59.999Z').toLocaleString()).toBe('12/31/2020, 11:59:59 PM'); | |
expect(new Date('2021-01-01T09:59:59.999Z').getDate()).toBe(31); // HST Day | |
expect(new Date('2021-01-01T09:59:59.999Z').getUTCDate()).toBe(1); // GMT Day | |
// ISO "Short" String - assumes GMT, no offset | |
expect(new Date('2021-01-01').toISOString()).toBe('2021-01-01T00:00:00.000Z'); // UTC no offset | |
expect(new Date('2021-01-01').toUTCString()).toBe('Fri, 01 Jan 2021 00:00:00 GMT'); | |
expect(new Date('2021-01-01').toLocaleString()).toBe('12/31/2020, 2:00:00 PM'); | |
expect(new Date('2021-01-01').getDate()).toBe(31); // HST Day | |
expect(new Date('2021-01-01').getUTCDate()).toBe(1); // GMT Day | |
// Non-ISO string - creates date based on current timezone (HST) - has offset | |
expect(new Date('01/01/2021').toISOString()).toBe('2021-01-01T10:00:00.000Z'); // UTC, note +10 hours - HST offset | |
expect(new Date('01/01/2021').toUTCString()).toBe('Fri, 01 Jan 2021 10:00:00 GMT'); | |
expect(new Date('01/01/2021').toLocaleString()).toBe('1/1/2021, 12:00:00 AM'); // User's Date | |
expect(new Date('01/01/2021').getDate()).toBe(1); // HST Day | |
expect(new Date('01/01/2021').getUTCDate()).toBe(1); // GMT Day | |
}); | |
it('show date-fns parse into date', () => { | |
// ISO Date String - outputs same as native | |
const isoFullNativeUTC = new Date('2021-01-01T09:59:59.999Z').toUTCString(); // 01 Jan 2021 09:59:59 GMT | |
const isoFullNativeLocale = new Date('2021-01-01T09:59:59.999Z').toLocaleString(); // 12/31/2020, 11:59:59 PM | |
expect(fnsParseISO('2021-01-01T09:59:59.999Z').toUTCString()).toEqual(isoFullNativeUTC); // Fri, 01 Jan 2021 09:59:59 GMT | |
expect(fnsParseISO('2021-01-01T09:59:59.999Z').toLocaleString()).toEqual(isoFullNativeLocale); // 12/31/2020, 11:59:59 PM | |
expect(fnsParseJSON('2021-01-01T09:59:59.999Z').toUTCString()).toEqual(isoFullNativeUTC); // Fri, 01 Jan 2021 09:59:59 GMT | |
expect(fnsParseJSON('2021-01-01T09:59:59.999Z').toLocaleString()).toEqual(isoFullNativeLocale); // 12/31/2020, 11:59:59 PM | |
// ISO Date String - not same as native | |
const nonIso = new Date('01/01/2021').toISOString(); // 2021-01-01T10:00:00.000Z -> Native: 2021-01-01T00:00:00.000Z | |
const nonIsoUTC = new Date('01/01/2021').toUTCString(); // Fri, 01 Jan 2021 10:00:00 GMT -> Native: Fri, 01 Jan 2021 00:00:00 GMT | |
const nonIsoLocale = new Date('01/01/2021').toLocaleString(); // 1/1/2021, 12:00:00 AM -> Native: 12/31/2020, 11:59:59 PM | |
// Assumes local timezone HST unlike native | |
expect(fnsParseISO('2021-01-01').toISOString()).toEqual(nonIso); // Same day - adds HST +10 offset | |
expect(fnsParseISO('2021-01-01').toUTCString()).toEqual(nonIsoUTC); // Same day - adds HST +10 offset | |
expect(fnsParseISO('2021-01-01').toLocaleString()).toEqual(nonIsoLocale); // Same day - Noon | |
expect(fnsParseISO('2021-01-01').toISOString()).not.toEqual(new Date('2021-01-01').toISOString()); // Native doesn't have +10 offset | |
expect(fnsParseISO('2021-01-01').toUTCString()).not.toEqual(new Date('2021-01-01').toUTCString()); | |
expect(fnsParseISO('2021-01-01').toLocaleString()).not.toEqual(new Date('2021-01-01').toLocaleString()); | |
}); | |
it('show date-fns format', () => { | |
expect(fnsParse('2021-01-01', "yyyy-MM-dd", new Date(Date.now()))).toEqual(new Date('2021-01-01T10:00:00.000Z')); // Same as parseISO | |
expect(fnsParse('01/01/2021', "MM/dd/yyyy", new Date(Date.now()))).toEqual(new Date('2021-01-01T10:00:00.000Z')); // Sam as Native | |
}); | |
it('show date-fns format into ISO String - non-UTC', () => { | |
// ISO Date String | |
expect(fnsFormatISO(new Date('2021-01-01T09:59:59.999Z'))).toBe('2020-12-31T23:59:59-10:00'); // Notice it added HST Timezone (10:00) | |
expect(fnsFormatISO(new Date('2021-01-01'))).toBe('2020-12-31T14:00:00-10:00'); // Same 2:00 PM HST | |
// Non-ISO string - creates date based on current timezone (HST) | |
expect(fnsFormatISO(new Date('01/01/2021'))).toBe('2021-01-01T00:00:00-10:00'); // User's Date | |
// ISO yyyy-mm-dd string | |
expect(fnsFormatISO(new Date('2021-01-01T09:59:59.999Z'), { representation: 'date' })).toBe('2020-12-31'); | |
expect(fnsFormatISO(new Date('2021-01-01'), { representation: 'date' })).toBe('2020-12-31'); | |
expect(fnsFormatISO(new Date('01/01/2021'), { representation: 'date' })).toBe('2021-01-01'); // User's Date | |
}); | |
it('show date-fns format', () => { | |
expect(fnsFormat(new Date('2021-01-01'), "yyyy-MM-dd")).toBe('2020-12-31'); // Day before - no offset | |
expect(fnsFormat(fnsParseISO('2021-01-01'), "yyyy-MM-dd")).toBe('2021-01-01'); | |
}); | |
it('should show correct date for user in Hawaii from a deadline entry in California', () => { | |
const date = fnsParseJSON('2021-01-01T08:08:00.000Z'); | |
expect(fmDate(date, 'short')).toBe('12/31/2020'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment