0 votes
by (220 points)
I know the macro exists in Harlowe -- does it exist in Sugarcube 2? I just want the story to display whatever the user's date or time is.



1 Answer

0 votes
by (154k points)
selected by
Best answer

NOTE: Your question doesn't state how you want the Date and/or Time formatted, nor does it state if you want that formatting to respect the Reader's country's (locale) default Date / Time formats or if you want everyone to see the same formats.

SugarCube doesn't have built-in Date / Time displaying macros, but it would be fairly easy to add your own custom macros/widgets to do that.

Basically you would create an instance of a Javascript Date object and store that within a (temporary) variable, then use the Date object's methods and some Javascript code to format either the Date or Time portions, which you would then output to the page. Which methods you use depends on how you want the different parts formated, and if you want to respect the Reader's locale settings.


If you want custom macros that work exactly like Harlowe 2's (current-date:) and (current-time:) macros then do the following:

1. Create a new Passage with whatever name you like (I used StoryWidgets) and assign that Passage a tag of widget

2. Add the follow markup to the above Passage, it creates the custom <<currentdate>> and <<currenttime>> macros.

<<widget "currentdate">>\
	<<print new Date().toDateString()>>\

<<widget "currenttime">>\
		_d to new Date(),
		_am to (_d.getHours() < 12),
		_hr to ((_d.getHours() % 12) || 12),
		_mins to (_d.getMinutes() < 10 ? "0" : "") + _d.getMinutes()>>\
		<<print _hr + ":" + _mins + " " + (_am ? "A" : "P") + "M">>\

3. Add markup like the following to one of your passages to use the new customer macros.

Date: <<currentdate>>
Time: <<currenttime>>