Let’s start by pointing out the obvious. It is painful to work with dates. No matter the technology, it is just harder than it should be.

Anyway… I stumbled across one of those annoying date related issues with axios. Just to be fair, the problem is not necessarily an axios problem, I just got to it when using axios. By default, axios will use JSON.stringify to convert an object to a json. Nothing fancy here. The problem is that when a date object is sent to the server, it will be converted to UTC and one might not necessarily want that.

In reading axios documentation, the recommendation was to provide the transformRequest which allows for complete control of the serialization. While that would certainly work, it is a lot of work to just to handle dates in a different way.

The simpler solution to this is to simply overwrite the toJSON function of the Date prototype. Like so:

This works because internally, the JSON.stringify function will call toJSON. Objectively, you could do this to any prototype, although none of the other types are as annoying as Date to work with.

Cheers, Lucas