airsdk / Adobe-Runtime-Support

Report, track and discuss issues in Adobe AIR. Monitored by Adobe - and HARMAN - and maintained by the AIR community.
200 stars 11 forks source link

[Bug] The "Magic Date" (31.07): `Date` object - strange bug where `Date` will not properly assign month the first time #3480

Closed 2jfw closed 7 hours ago

2jfw commented 7 hours ago

I have encountered a very strange behavior of the Date object, when the local machine/PC date (Windows 10, Version @10.0.19045 Build 19045, AIR 50.2.4) is set to 31.07.2024 and the date object then has its date changed: It will state a wrong month (behind the scenes: setting month is ignored)

It's easier to show sample code:

Run this code without changing your local machine's date:

var date : Date = new Date();

date.fullYear = 2024;
date.month    = 5; // June (!)
date.date     = 26;
trace(date);

date.fullYear = 2024;
date.month    = 5; // June (!)
date.date     = 26;
trace(date);

You will see the following correct output: [Of course hh:mm:ss will be different when you run the code]

[trace] Wed Jun 26 16:05:06 GMT+0200 2024
[trace] Wed Jun 26 16:05:06 GMT+0200 2024

Now change your local machine's date to 31.07.2024 and run the code again. You will see:

[trace] Fri Jul 26 16:09:26 GMT+0200 2024
[trace] Wed Jun 26 16:09:26 GMT+0200 2024

As you see, the initial trace of the date incorrectly states "July" (6) instead of "June" (5). In the debugger, you can see that initially assigning the month 5 will keep the date object's month on 6. Only second time it will become 5.

31st-july-month-issue [Tooltip from IDE]

This only happens when your local machine's date was set to 31.07. Changing the year does not affect the incorrect output.

🧙💬 So, what is the "magic" behind the date 31.07 ? It seems like a really "strange" issue to me...

ajwfrost commented 7 hours ago

I remember seeing this effect for the first time and puzzling over it ... but what you're forgetting is this part:

var date : Date = new Date();

This will set the date to the machine's current date/time. So, this value is the 31st July.

Then:

date.month    = 5; // June (!)

will change the month to June. So, the date is now, 31st June. BUT that date doesn't exist, so the Date object internally will actually be 1st July. So then:

date.date     = 26;

means the date value goes to 26, so we're now at 26th July.

Next time, when you set the month back to June, 26th June is still valid so it works...

So to avoid this, make sure you change the date value before the month value!

var date : Date = new Date();
date.date     = 26;
date.month    = 5; // June (!)
date.fullYear = 2024;
trace(date);
2jfw commented 7 hours ago

Well, not so magical after all ... 🤨 Thanks for bringing this to attention and the insight on this, Andrew!