question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
atiertantcommented, Jun 26, 2019

@deskoh yes but what about Buffer… couldn’t we have the same behavior ?

0reactions
kputhcommented, Feb 17, 2022

I think the API documentation of the sending and receiving methods should state that data is serialized and deserialized using JSON.stringify and JSON.parse. These two functions have some suprising behaviour: they do not only replace Dates with strings, but also strip undefined-valued properties from objects and replace undefined 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found