File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -50,22 +50,38 @@ const arraySchemaCJS = {
50
50
items : schemaCJS
51
51
}
52
52
53
+ const dateFormatSchema = {
54
+ description : 'Date of birth' ,
55
+ type : 'string' ,
56
+ format : 'date'
57
+ }
58
+
59
+ const dateFormatSchemaCJS = {
60
+ description : 'Date of birth' ,
61
+ type : 'string' ,
62
+ format : 'date'
63
+ }
64
+
53
65
const obj = {
54
66
firstName : 'Matteo' ,
55
67
lastName : 'Collina' ,
56
68
age : 32
57
69
}
58
70
71
+ const date = new Date ( )
72
+
59
73
const multiArray = [ ]
60
74
61
75
const CJS = require ( 'compile-json-stringify' )
62
76
const CJSStringify = CJS ( schemaCJS )
63
77
const CJSStringifyArray = CJS ( arraySchemaCJS )
78
+ const CJSStringifyDate = CJS ( dateFormatSchemaCJS )
64
79
const CJSStringifyString = CJS ( { type : 'string' } )
65
80
66
81
const FJS = require ( '.' )
67
82
const stringify = FJS ( schema )
68
83
const stringifyArray = FJS ( arraySchema )
84
+ const stringifyDate = FJS ( dateFormatSchema )
69
85
const stringifyString = FJS ( { type : 'string' } )
70
86
let str = ''
71
87
@@ -138,6 +154,18 @@ suite.add('compile-json-stringify obj', function () {
138
154
CJSStringify ( obj )
139
155
} )
140
156
157
+ suite . add ( 'JSON stringify date' , function ( ) {
158
+ JSON . stringify ( date )
159
+ } )
160
+
161
+ suite . add ( 'fast-json-stringify date format' , function ( ) {
162
+ stringifyDate ( date )
163
+ } )
164
+
165
+ suite . add ( 'compile-json-stringify date format' , function ( ) {
166
+ CJSStringifyDate ( date )
167
+ } )
168
+
141
169
suite . on ( 'cycle' , cycle )
142
170
143
171
suite . run ( )
Original file line number Diff line number Diff line change @@ -285,10 +285,7 @@ function $asDatetime (date, skipQuotes) {
285
285
function $asDate (date, skipQuotes) {
286
286
const quotes = skipQuotes === true ? '' : '"'
287
287
if (date instanceof Date) {
288
- const year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(date)
289
- const month = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(date)
290
- const day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date)
291
- return quotes + year + '-' + month + '-' + day + quotes
288
+ return quotes + new Date(date.getTime() - (date.getTimezoneOffset() * 60000 )).toISOString().slice(0, 10) + quotes
292
289
} else if (date && typeof date.format === 'function') {
293
290
return quotes + date.format('YYYY-MM-DD') + quotes
294
291
} else {
You can’t perform that action at this time.
0 commit comments