+1 vote
by (390 points)
Hi, I'm new here and I would like to implement time and date in my game, but I can't get it to work. So what I want is the in-game time and date to display in the story-caption like so:

Date: 1 jan 2017
Time: 12:00

And that certain actions in game take a certain amount of time.

So how could I do this? What code can I use and where do I put it?

PS: I don't know much about code, only made a few simple websites in html.

1 Answer

+4 votes
by (390 points)
selected by
Best answer

I found everything I need on the forum I think

I'll paste this in a widget:

	Date & Time Widget Setup
	/* This must be set to whatever the initial game date/time should be. */
	$gameDate to new Date("2015-03-17T03:24Z"); /* Must use UTC time. */
	window.GameDays to [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
	window.GameMonths to [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"

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

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

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

	Date & Time Printing Widget Definitions
/* Prints the current date ("{weekday} {month} {day}, {year}"). */
<<widget "date">>\
<<print String.format("{0} {1} {2}, {3}",

/* Prints the current time (12H). */
<<widget "time12h">>\
<<if $gameDate.getUTCHours() eq 0>>\
<<elseif $gameDate.getUTCHours() gt 12>>\
<<print $gameDate.getUTCHours() - 12>>\
<<print $gameDate.getUTCHours()>>\
<<if $gameDate.getUTCMinutes() lt 10>>0<</if>><<print $gameDate.getUTCMinutes()>> \
<<if $gameDate.getUTCHours() gte 12>>PM<<else>>AM<</if>>\

/* Prints the current time (24H). */
<<widget "time24h">>\
<<if $gameDate.getUTCHours() lt 10>>0<</if>><<print $gameDate.getUTCHours()>>:\
<<if $gameDate.getUTCMinutes() lt 10>>0<</if>><<print $gameDate.getUTCMinutes()>>\

/* Prints the current date and time (12H). */
<<widget "datetime">><<date>> <<time12h>><</widget>>

This to print the time and date:

''Date:'' <<date>>
''Time:'' <<time12h>> (12H) / <<time24h>> (24H)
''Both:'' <<datetime>> (12H)

This to add time:

/* Adds 5 minutes. */
<<addmins 5>>

/* Adds 5 hours. */
<<addhours 5>>

/* Adds 5 days. */
<<adddays 5>>

And this to set shop hours:

<<if $gameDate.getUTCHours() gte 8 and $gameDate.getUTCHours() lte 17>>Open<<else>>Closed<</if>>

Just posting this for verification and if other people are looking for this.

by (6.2k points)
Yes in future please always check this Q&A and the old forums, then twine websites and youtube and stuff first. Asking a question here should be your last resort.
by (390 points)
It wasn't particularly easy to find and I still needed to figure out how to use it. I never studied coding, only a little bit of html.
by (390 points)

I successfully implemented this widget in my game, the date and time flow as expected, But how do i refer to a specific day or days?

I tried making some if statements, none of them show an error, but none of them work as well.

\<<if $gameDate.getUTCDay() gte "mon">>
  It's closed.


\<<if $window.GameDays is "mon">>
  It's closed.

Btw, i'm using twine 2.2.1 and sugarcube 2.21.

Can anyone help me out?

by (100 points)
Only started using Twine a couple of days back. Just wanted to say this worked for me.

<<if GameDays[$gameDate.getDay()] is "Mon">> Open
<<elseif GameDays[$gameDate.getDay()] neq "Mon">> Closed
by (110 points)
using this how could I trigger an event on a specific date such as for a birthday for example?

I've been playing around with it for ages and I seem to be getting nowhere

(using twine 1.4.2 and sugarcube 2)