+1 vote
by (630 points)
I'm using Twine 2.1.3 on Mac OS with Harlowe 2.0.1

Is it possible to manipulate the result from (current-date:) and (current-time:) using other macros?

For example, if (current-date:) gives me: Mon Jun 12 2017

Is there a way to make it show Tues June 13 2017?   etc.?

Same question for time.  I want the character to refer to a point in the near future as it relates to the real world player.

I know I can do this the long way by parsing / replacing text from the string, but I'm hoping there's some addition/subtraction function that can work with dates that I'm missing...  Thanks!

1 Answer

+2 votes
by (159k points)
edited by
 
Best answer

As stated in the relevant Harlowe documentation both of those macros return a String value, and based on the related Javascript source code there is no way to access the unique Javascript Date object created for each call of those macros.

Unfortunately Harlowe has no built-in support for the Date datatype, and while you could use Javascript in your Story Javascript area to create your own functions to generate the required output Harlowe has limited support for using your own functions.

NOTE: The following example demonstrates one way to create your own Javascript function and one way to use that function within a Passage, it is NOT a recommendation on best way to create a namespace nor to implement the type of function you requested.

1. Use code like the following to define your own Namespace (and your own custom function within that namespace. The code needs to be placed within the Story Javascript area

/* Define a unique namespace to contain your custom functionallity. */
if (! window.GE) {
	window.GE = {};
}

/* Add a custom function to your unique namespace. */
GE.getNowStr = function () {
	// Constants.
	var dayNames = ["Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"];
	var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	
	var now = new Date();

	// format: Tues June 13 2017
	var str = dayNames[now.getDay()] + " " +
			monthNames[now.getMonth()] + " " +
			now.getDate() + " " +
			now.getFullYear();
	return str;
};

2. Use code like the following with a Passage to call the custom function.

now: (print: GE.getNowStr())

 

edit: Just fixed formatting of code example.

by (630 points)
Thank you for taking the time to provide that example.  Huge help!
...