0 votes
by (170 points)

Hi,

I've been trying to use a date and time tool/widget. Mainly to show it on the sidebar as so:

Time: <<time24h>>
Date: <<date>>

I've also removed the year from the widget since I did not need it and it worked perfectly:

<<print String.format("{0} {1} {2}, {3}",
	GameDays[$gameDate.getDay()],
	GameMonths[$gameDate.getMonth()],
	$gameDate.getDate(),
)>>\

I can add days, hours and so on, but I've tried to create a way to set the time so that it can be more flexible. Like so:

/* Date & Time Setting Widget Definitions */
/* Sets the specified number of minutes. */
<<widget "setmins">>\
<<run $gameDate.setUTCMinutes($gameDate.setUTCMinutes() + $args[0])>>\
<</widget>>

/* Sets the specified number of hours. */
<<widget "sethours">>\
<<run $gameDate.setUTCHours($gameDate.setUTCHours() + $args[0])>>\
<</widget>>

/* Sets the specified number of days. */
<<widget "setdays">>\
<<run $gameDate.setUTCHours($gameDate.setUTCHours() + $args[0] * 24)>>\
<</widget>>

It's, obviously, mainly a copy/paster of the advance time of Mad Exile's /* Adds ... */. I thought it could work, but it displays this:

Time: :
Date: NaN,

The character goes to sleep. So I need a day to pass and the time to always be 8:00 (24h). The code looks like this:

/* Sets hours */
<<sethours 08>>
/* Sets minutes */
<<setmins 00>>

/* Adds 1 day-Pass time */
<<adddays 1>>

Adding things works like a charm. But it seems like I'm far from knowing how to change the "add" command into a "set" one.

I hope that's clear enough and that it isn't too much to ask for. I can't figure out how to solve it, but something tells me it's simple when I look at the code I'm using for reference (not that I'd be able to create it...).

1 Answer

+1 vote
by (44.7k points)
selected by
 
Best answer

Since you appear to be using a Date object, you can set the date and time like this:

	<<set $gameDate.setDate($gameDate.getDate() + 1)>>
	<<set $gameDate.setHours(8)>>
	<<set $gameDate.setMinutes(0)>>
	<<set $gameDate.setSeconds(0)>>

That would increase the day by one, and set the time to 8:00 AM.  (Be careful not to use that after midnight or you'll skip a day.)

If you know what day of the current month you want to set it to, then you can use .setDate() to set that day like this:

	<<set $gameDate.setDate($dayOfMonth)>>

If $dayOfMonth exceeds the number of days in the current month, then that will change the month to the next month.  So you could prevent the "skip a day" problem like this:

<<nobr>>  /* A new day. */
	<<set $dayOfMonth += 1>>  /* increment the day of the month variable */
	<<set _CurMonth = $gameDate.getMonth()>>  /* get the current month */
	<<set $gameDate.setDate($dayOfMonth)>>  /* change the day */
	<<if _CurMonth != $gameDate.getMonth()>>  /* if the month changed... */
		<<set $dayOfMonth = $gameDate.getDate()>>  /* ...then update the day of month */
	<</if>>
	<<set $gameDate.setHours(8)>>  /* set hour to 8 AM */
	<<set $gameDate.setMinutes(0)>>  /* set minutes to 0 */
	<<set $gameDate.setSeconds(0)>>  /* set seconds to 0 */
<</nobr>>

That will set the day based on the value of $dayOfMonth, and thus make sure that you don't accidentally skip a day.

If you're interested I have some other "Time" sample code here which may help.  (Click "Jump to Start" on the UI bar to see other sample code there.)

Hope that helps!  :-)

by (170 points)
Thank you for the fast and appropriate answer!

Maybe I'll end up facing some other difficulties, but that enables me to finally move forward.
...