Relative Content

Tag Archive for javascriptdatetime

How to mock the current date/time in JS?

I want to be able to mock the current date/time used by new Date() or in any other built-in date function in JS. This is so I can test our application in exactly the same state every time (since a lot of things change depending on the current date).

Strange behaviour with dates

I am programming a C#/React application, and I am getting a strange behaviour in the vanilla JavaScript. When I pass the date 01/05/2024 to the client browser in the format of double, using:

How do I convert milliseconds into a date object?

I am trying to make a function that is, effectively, the opposite of getTime(); it takes a number of milliseconds as an input and converts the number into years, days, hours, seconds, and milliseconds, and then returns a Date object with those values. The conversion works perfectly, but when I try to create the Date object, things go wrong.