By default, JSON.stringify will convert Dates to a timestamp as if toISOString was called on the date.
If you want dates in some other format, you can use a replacer function that tests for a value that is a timestamp in the same format as that produced by JSON.stringify (i.e. YYYY-MM-DDTHH:mm:ss.sssZ) and instead stringify it to some other format, e.g. using Date.prototype.toString.
Note that you have to test for a formatted string as the value will be stringified before being passed to the replacer function.
// Test if value is a string in the same format as that produced
// by toISOString
function isTimestamp(value) {
return /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/.test(value);
}
// Replace timestamps in toISOString format with
// timestamps in toString format
function replacer (key, value) {
console.log(Object.prototype.toString.call(this[key]));
if (isTimestamp(value)) {
return this[key].toString();
} else {
return value;
}
}
// Sample date
let startTime = new Date(2020,8,8,16);
// Sample object
let data = {start: startTime};
// Stringify date using toString
console.log(JSON.stringify(data, replacer))
Alternatively, you can test the value of this[key]
within the the replacer and not use the isTimestamp function, e.g.:
if (Object.prototype.toString.call(this[key]) == '[object Date]') {
// return custom format
}
Using moment.js you might do:
// Replace timestamps in toISOString format with
// timestamps in some other format
function replacer(key, value) {
// Strict parse of value
let d = moment(value, 'YYYY-MM-DDTHH:mm:ss.sssZ', true);
// If it's a valid Date
if (d.isValid()) {
// Return whatever format you like
return moment(this[key]).format('YYYY-MM-DD HH:mm ZZ');
} else {
// Otherwise just return the default value
return value;
}
}
// Sample date
let startTime = new Date(2020, 8, 8, 16);
// Sample object
let data = {start: startTime};
// Stringify date using toString
console.log(JSON.stringify(data, replacer))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>