How Do I Calculate Day Names From Parameters?

Status
Not open for further replies.

lordvalinar

Lord of the Damned
Veteran
Joined
Mar 31, 2013
Messages
249
Reaction score
111
First Language
English
Primarily Uses
RMMZ
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

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
770
Reaction score
420
First Language
Poland
Primarily Uses
RMMV
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
Veteran
Joined
Aug 2, 2013
Messages
1,492
Reaction score
408
First Language
German
Primarily Uses
RMMV
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
Veteran
Joined
Mar 31, 2013
Messages
249
Reaction score
111
First Language
English
Primarily Uses
RMMZ
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

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
530
Reaction score
232
First Language
English
Primarily Uses
RMMV
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
Veteran
Joined
Mar 31, 2013
Messages
249
Reaction score
111
First Language
English
Primarily Uses
RMMZ
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
Veteran
Joined
Jun 6, 2020
Messages
530
Reaction score
232
First Language
English
Primarily Uses
RMMV
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
Veteran
Joined
Jun 30, 2012
Messages
8,562
Reaction score
3,832
First Language
English
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
Veteran
Joined
Aug 2, 2013
Messages
1,492
Reaction score
408
First Language
German
Primarily Uses
RMMV
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
Global Mod
Joined
Sep 6, 2012
Messages
7,590
Reaction score
5,146
First Language
Dutch
Primarily Uses
RMXP

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Ami
--- Diary ---

M.Mage: It's the F.Mage's Diary. While she isn't here,i can read it.

May, 10: I'm hurt after the battle with the Minotaur. But luckily,F.Healer heal me with her Heal-2. That why,i Fall in Love with her.

M.Mage: Eh???
So... some of my Desktop hardware has kicked it apparently (still trying to figure out what and how at the moment :/ ) .... yay?
Stream will be live shortly with some Darkest Dungeon! Feel free to drop by!
Made a HUGE (YYOOOOJJ) Update to Monstructs and moving towards a Steam Early Access release!
A skill type called: "Rumagic". The intention is Magic with Rum(that pirates drink)
Does it sound strange in English?

Forum statistics

Threads
104,221
Messages
1,004,799
Members
135,736
Latest member
moenimael
Top