Date are stringifyed
See original GitHub issue- report a bug
- request a feature
sending a Date object is stringifyed
Steps to reproduce
var server = require('socket.io').listen(4000);
var client = require('socket.io-client').connect('http://localhost:4000');
server.on('connection', function(socket) {
socket.on('message', function(msg) {
console.log(typeof msg.myDate); //string
});
});
client.on('connect', function() {
client.emit('message', { myDate: new Date() });
});
Expected behaviour
shoud receive a Date Object
Setup
- OS: Fedora
- browser: Node.js
- socket.io version: 2.2.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:10 (3 by maintainers)
Top Results From Across the Web
javascript - Issues with Date() when using JSON.stringify() and ...
The Date object's constructor can take a date string, so you can turn those string values back into dates by doing: var x...
Read more >json-stringify-date - npm
Executes exactly the JSON.stringify , but can preserve time zones in dates. parse. stringifyDate.parse(text [, reviver]).
Read more >JSON.stringify() - JavaScript - MDN Web Docs
Date objects implement the toJSON() method which returns a string (the same as date.toISOString() ). Thus, they will be stringified as ...
Read more >Date and JSON.stringify() - CodeProject
stringify () I have written a code to stringify an object in JSON format using jquery. var o = new Object(); o.
Read more >How to parse a JSON Date in JavaScript | bobbyhadz
To parse a JSON date, use the `toJSON()` method to get a string representation of the `Date` object. Pass the JSON string to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@deskoh yes but what about Buffer… couldn’t we have the same behavior ?
I think the API documentation of the sending and receiving methods should state that data is serialized and deserialized using
JSON.stringify
andJSON.parse
. These two functions have some suprising behaviour: they do not only replace Dates with strings, but also stripundefined
-valued properties from objects and replaceundefined
with null in arrays. It may not seem like a big deal, but it implied that types for sending and receiving the same data may not line up at runtime and in TypeScript.