Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading