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:
<code>public class Generic
{
public static double DateToJS(DateTime dt) => (dt - new DateTime(1970, 1, 1).Date).TotalMilliseconds;
}
</code>
<code>public class Generic
{
public static double DateToJS(DateTime dt) => (dt - new DateTime(1970, 1, 1).Date).TotalMilliseconds;
}
</code>
public class Generic
{
public static double DateToJS(DateTime dt) => (dt - new DateTime(1970, 1, 1).Date).TotalMilliseconds;
}
and then, in the client, running the following JavaScript snippet:
<code>const dayGiven = new Date(this.state.data.date); // Date passed from Back-End (double)
const dayGivenDate = new Date(dayGiven.getFullYear(), dayGiven.getMonth(), dayGiven.getDate());
const SOMonth = new Date(dayGivenDate.getFullYear(), dayGivenDate.getMonth(), 1);
const nDaysMonth = new Date(dayGiven.getFullYear(), dayGiven.getMonth() + 1, 0).getDate();
const nDaysTillEOM = nDaysMonth - dayGiven.getDate() + 1;
</code>
<code>const dayGiven = new Date(this.state.data.date); // Date passed from Back-End (double)
const dayGivenDate = new Date(dayGiven.getFullYear(), dayGiven.getMonth(), dayGiven.getDate());
const SOMonth = new Date(dayGivenDate.getFullYear(), dayGivenDate.getMonth(), 1);
const nDaysMonth = new Date(dayGiven.getFullYear(), dayGiven.getMonth() + 1, 0).getDate();
const nDaysTillEOM = nDaysMonth - dayGiven.getDate() + 1;
</code>
const dayGiven = new Date(this.state.data.date); // Date passed from Back-End (double)
const dayGivenDate = new Date(dayGiven.getFullYear(), dayGiven.getMonth(), dayGiven.getDate());
const SOMonth = new Date(dayGivenDate.getFullYear(), dayGivenDate.getMonth(), 1);
const nDaysMonth = new Date(dayGiven.getFullYear(), dayGiven.getMonth() + 1, 0).getDate();
const nDaysTillEOM = nDaysMonth - dayGiven.getDate() + 1;
I get a result in SOMonth.toISOString().split('T')[0]
(start-of-month) of 30/04/2024.
What can I do? How to address this problem in JavaScript?
Also, if I run the following JavaScript snippet:
<code>day=new Date(2024,5-1,1) // May
//>> Wed May 01 2024 00:00:00 GMT+0100 (Western European Summer Time)
SOMonth = new Date(day.getFullYear(), day.getMonth(), 1);
//>> Wed May 01 2024 00:00:00 GMT+0100 (Western European Summer Time)
SOMonth.toISOString().split('T')[0]
//>> '2024-04-30' => Wrong date. I would like to obtain May 1 2024
</code>
<code>day=new Date(2024,5-1,1) // May
//>> Wed May 01 2024 00:00:00 GMT+0100 (Western European Summer Time)
SOMonth = new Date(day.getFullYear(), day.getMonth(), 1);
//>> Wed May 01 2024 00:00:00 GMT+0100 (Western European Summer Time)
SOMonth.toISOString().split('T')[0]
//>> '2024-04-30' => Wrong date. I would like to obtain May 1 2024
</code>
day=new Date(2024,5-1,1) // May
//>> Wed May 01 2024 00:00:00 GMT+0100 (Western European Summer Time)
SOMonth = new Date(day.getFullYear(), day.getMonth(), 1);
//>> Wed May 01 2024 00:00:00 GMT+0100 (Western European Summer Time)
SOMonth.toISOString().split('T')[0]
//>> '2024-04-30' => Wrong date. I would like to obtain May 1 2024
1