diff --git a/lib/serializer.js b/lib/serializer.js index 1b3a77d8..acd3cdc4 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -108,7 +108,7 @@ module.exports = class Serializer { asDate (date) { if (date === null) return '""' if (date instanceof Date) { - return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"' + return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, -14) + '"' } if (typeof date === 'string') { return asString(date) @@ -119,7 +119,7 @@ module.exports = class Serializer { asTime (date) { if (date === null) return '""' if (date instanceof Date) { - return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"' + return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(-13, -5) + '"' } if (typeof date === 'string') { return asString(date) diff --git a/test/date.test.js b/test/date.test.js index 79f21b3d..baf874e4 100644 --- a/test/date.test.js +++ b/test/date.test.js @@ -6,6 +6,18 @@ const build = require('..') process.env.TZ = 'UTC' +for (const iso of ['+010000-02-03T04:05:06.000Z', '-000001-02-03T04:05:06.000Z', '0000-02-03T04:05:06.000Z', '9999-02-03T04:05:06.000Z']) { + test(`preserve the whole calendar date for ${iso}`, (t) => { + const stringify = build({ type: 'string', format: 'date' }) + t.assert.equal(stringify(new Date(iso)), JSON.stringify(iso.split('T')[0])) + }) + + test(`preserve the time for ${iso}`, (t) => { + const stringify = build({ type: 'string', format: 'time' }) + t.assert.equal(stringify(new Date(iso)), '"04:05:06"') + }) +} + test('render a date in a string as JSON', (t) => { t.plan(2)