# How Do I Calculate Day Names From Parameters?

Status
Not open for further replies.

#### lordvalinar

##### Lord of the Damned
I'm creating my own Time System plugin and I'm trying to figure how to calculate the current day name based on the several factors (namely the plugin parameters). The attached screenshot is an example if the parameters read:

Days Per Month: 31
Months Per Year: 12 (only showing 3 though to keep it simple/smaller)
Day Names: ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]

So how do I get a function to return "Thu" based on the 1st of Month 2 in the example? (Or if the Days per Month were to be changed to 28), how to return "Mon" based on the 1st of Month 2, etc.

- To anyone before commenting "Why not use an already made time system plugin?"
A: While great in their own way, I am designing my own plugin to both tailor it to my needs, and I'm learning as I develop it. Enhancing it when/where I can (like I'm just now getting into the clock sprite side of it) and so the other systems don't really have what I need. So that's where I'm at with that.

#### Attachments

• 5.7 KB Views: 3

#### Soulrender

You could use the Date.getDay() method, which returns 0 for sunday, up to 6 for saturday. So, you could simply create an array with the name for the day names:

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date(dateString);
var dayName = days[d.getDay()];
Here dateString is the string you received from the third party API.

Alternatively, if you want the first 3 letters of the day name, you could use the Date object's built-in toString method:

var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];
That will take the first word in the d.toString() output, which will be the 3-letter day name.

#### Bex

##### Veteran
Variable 1 = Amount of Total Days passed.
Variable 1 mod 7
If Variable 1 = 0
Show Text: Today is Monday
If Variable 1 = 1
Show Text: Today is Tuesday
If Variable 1 = 2
Show Text: Today is Wednesday
If Variable 1 = 3
Show Text: Today is Thursday
If Variable 1 = 4
Show Text: Today is Friday
If Variable 1 = 5
Show Text: Today is Saturday
If Variable 1 = 6
Show Text: Today is Sunday

Edit:
Example:
Day 14 mod 7 = 0 ; 0 equals Monday
Day 16 mod 7 = 2 ; 2 equals Wednesday
Day 86 mod 7 = 2 ; 2 equals Wednesday
...

Last edited:

#### lordvalinar

##### Lord of the Damned
Got it to work, through this mess (it was sometimes returning an index greater than 7, and kept returning the day as "undefined", but this fixed everything:

#### Attachments

• 4.8 KB Views: 7

#### Solar_Flare

##### Veteran
You could use the Date.getDay() method, which returns 0 for sunday, up to 6 for saturday. So, you could simply create an array with the name for the day names:

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var d = new Date(dateString);
var dayName = days[d.getDay()];
Here dateString is the string you received from the third party API.

Alternatively, if you want the first 3 letters of the day name, you could use the Date object's built-in toString method:

var d = new Date(dateString);
var dayName = d.toString().split(' ')[0];
That will take the first word in the d.toString() output, which will be the 3-letter day name.
That's a great example, but it relates to real time and as such probably isn't what the original poster is looking for.

Got it to work, through this mess (it was sometimes returning an index greater than 7, and kept returning the day as "undefined", but this fixed everything:
That code looks incorrect. If you take "day of month" mod "days per week", then every month is going to begin on a Monday.

To get the "day of week", you need to take "day of year" mod "days per week".

(Although even that is wrong for the Gregorian calendar, as it makes every year start on a Monday; but it might be close enough for a game? If it's not close enough you can take "days since game started" mod "day of year".)

#### lordvalinar

##### Lord of the Damned
That's a great example, but it relates to real time and as such probably isn't what the original poster is looking for.

That code looks incorrect. If you take "day of month" mod "days per week", then every month is going to begin on a Monday.

To get the "day of week", you need to take "day of year" mod "days per week".

(Although even that is wrong for the Gregorian calendar, as it makes every year start on a Monday; but it might be close enough for a game? If it's not close enough you can take "days since game started" mod "day of year".)
It's correct. Tested it myself, and it works. ((Although I did notice my example calendar is incorrect since I skipped day 10 on Month 2...))

Days Per Month: 31
Day Names: ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]

The code:

JavaScript:
``````// Getting day 28 on Month 1 (of the above calendar)
Game_TimeSystem.prototype.weekDayName = function() {
var week = \$.dayNames.length; // 7 days.. (to die)
var mod  = \$.dayPerMo % week; // 31 % 7 = 3
var startDay = (this._timeMonth - 1) * mod; // (1-1) * 0 = 0
var index = ((this._timeDay - 1) % week) + startDay; // (28-1) % 7 = 6
while (index >= week) index -= week; // no adjustments
return \$.dayNames[index]; // should return "Sun" (index 6)
};``````
Edit: Then to show you the formula works ongoing (Day 9 of Month 2 above - since I screwed up Day 10)

JavaScript:
``````Game_TimeSystem.prototype.weekDayName = function() {
var week = \$.dayNames.length; // 7 (still)
var mod  = \$.dayPerMo % week; // 3
var startDay = (this._timeMonth - 1) * mod; // (2-1) * 3 = 3
var index = ((this._timeDay - 1) % week) + startDay; //((9-1) % 7) + 3 = 4
while (index >= week) index -= week; // no adjustments
return \$.dayNames[index]; // should return "Fri" (index 4)
};``````

Last edited:

#### Solar_Flare

##### Veteran
Hmm, okay, maybe it's correct, but I think it's more complicated than it needs to be. I think something like this would still work and be both simpler and faster:

JavaScript:
``return ((this._timeMonth * \$.dayPerMo) + this._timeDay) % \$.dayNames.length;``
Untested, could be off by one or something. Also assumes that month 1 starts on a Monday. Basically the idea of that code is to calculate how many days have passed since the start of the year and then take that modulo 7. Of course, you could write that in several statements to make it even clearer, if you wanted to.

Or you could just stick with what you have, since it sounds like it works for you.

#### Tsukihime

##### Veteran
Following from the discussion on discord, I revised my day of month method from

Code:
``````function getDayOfMonth(day) {
return day % Days_Per_Month;;
}``````
to

Code:
``````function getDayOfMonth(day) {
var res = day % Days_Per_Month;
if (res == 0) {
res = Days_Per_Month
}
return res;
}``````
Which is still quite ugly, but I couldn't figure out how to deal with the case when the number of days divides evenly into it.

#### Bex

##### Veteran
Iam no Scripter, but iam curious why you are Focussing on the Month?

He wants the Weekday Name, isnt all that needed the total amount of Days passed?
Bringing the Month into this seems unnecessary and complicates this even more. Or am i wrong?

OP does your Time System have a Variable that stores the Total amount of passed Time?
The standard Game Time for example is in "Seconds"
Total Seconds devided through 86400 (Seconds a Day got) = Total Days.

#### slimmmeiske2

##### Little Red Riding Hood

Status
Not open for further replies.

### Latest Profile Posts

Hello I buyed RPG Maker VX Ace a long ago and I want to use it again but in the meantime I changed my pc but I lost the paper with my product key on so I can't use RPG maker again... someone can help me please ? or there is no hopes ?:'( ( I have a bad english I'm sorry )
I was away this days here in the forum but it was for a good cause!
The trailer of the demo of Luke Inn is on youtube and I am so happy!
I need to see the bugs of the game but I am happy that I have new content to show!

The last part of the modern interior will be ready soon. This will be a B-tile. With interior items for the kitchen, bathroom, toilet and storage room.
This frantic shooting ARPG plugin for MZ looks really great. Here is the link to a post from the official Japanese RPG Maker forum.