So I just answered to a member of the forum, on a nice question.
"Hey I want the game to know the real date, cause I can do cool things with that, like a Christmas event if it's Christmas in real life".
How could I resist to such a clever idea.
Searched for a plugin, found nothing.
Go 0figure, it is super simple. OR NOT?
Here comes the code:
// The actual calculation part:
var dt = new Date();
// The grabbing part
m = dt.getMonth();
d = dt.getDay();
y = dt.getYear();
// The correction part:
m = m+1;
d = d+4;
y = y+1900;
// The show part:
$gameVariables.setValue(1, m);
$gameVariables.setValue(2, d);
$gameVariables.setValue(3, y);
Fact 1: Okay I get it. Year 1900 is equivalent to 0.
Question 1: Why should I have to add 1 to month?
Question 2: Why should I add +4 to the day?
Question3: Is it supposed to work like that?
Nightmare to figure our date. Bugs or a features?
● ARCHIVED · READ-ONLY
-
-
The reason is, every programming language starts the first thing from 0. Arrays start from arr[0] as their first member, string[0] is the first character of the string, etc. The reason why is that is simply the fact that in past we had to conserve. Nowadays it's not so necessary, but it stayed as a convention.Question 1: Why should I have to add 1 to month?
Because getDay means day of the week. Not calendar day. So technically you shouldn't add +4, you should do it differently :pQuestion 2: Why should I add +4 to the day?
No. getYear() has been deprecated and you need to use getFullYear () instead. Your equation was therefore undefined + 1900, which Javascript treats as 0+1900. The reason why your game variable would still show 0 is simply because the game will write a variable value if it's truthy (in other words it is not: 0, null, NaN, undefined or false). If it is falsey, it will not write it. Also, instantiating a date usingFact 1: Okay I get it. Year 1900 is equivalent to 0.
new Date() without a suitable parameter will instantiate current date, not the 1900 date you were searching for.
In order to figure our dates, check this site:
https://www.w3schools.com/js/js_dates.asp -
Oh, of course it iterates from 0. I thought programmers would had changed that on a date function for convenience. But no. :p
"Because getDay means day of the week."
I was like:
Oh! (Sweating and turning red)
Embarassing!
Then enraged. Why the hell should I get THAT from 'day'? They should had named that weekday or something. Geez! :p :p :p
Thanks for mentioning that!
"So technically you shouldn't add +4, you should do it differently"
d = dt.getDate();
indeed.
getYear() returned 118! That was the whole problem! :p
But it makes no difference really. Since it is deprecated and if getFullYear () does the trick, I will use that!
@Aloe Guvner also helped me out, on that thread (the reason I asked what was going on)
https://forums.rpgmakerweb.com/index.php?threads/holiday-events.101916/#post-913737
P.S.
First time i find a no argument constructor so useful! :p
var d = new Date();