Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Clocks still driving me nuts

Made with Twine & SugarCube
SugarCube (v2.0.0-beta.4)

So I've been messing around with every type of clock on here for the last week. Most I've figured out, but the base java one I still can't figure out.
<<set $now to new Date(Date.now())>>
<<set $now to new Date("2009-05-08T08:00")>>

now: <<print $now>>
year: <<print $now.getYear()>>
month: <<print $now.getMonth()>>
day: <<print $now.getDay()>>
hours: <<print $now.getHours()>>
minutes: <<print $now.getMinutes()>>
seconds: <<print $now.getSeconds()>>

1) How would I add time manually to this one (+ 15min) or (8 hours) etc.

2) Or to add a set time per click?

3) I'd like to drop the GMT-0500 (Central Daylight Time) from the StoryCaption, but from what I read on the Java sites it seems more involved than its worth. Is there an easy way to not populate it? I know the T in "2009-05-08T08:00" is related to it, is there a letter like X or something to drop it?


I've seriously spent a ridiculous amount of time on something so simple. I think I wrote the 10k of dialogue in less time.

Thanks

Oh, PS: Where do I check these as 'Answered?'
«1

Comments

  • Firstly, it's JavaScript, not Java. Save for an unfortunate name similarity, owing to marketing shenanigans, and the fact that they both inherit from the C family of languages syntactically, the two languages are completely unrelated.

    Secondly, the following is pointlessly calling Date.now():
    <<set $now to new Date(Date.now())>>
    
    The Date object's constructor will default to the current date/time if no arguments are given, like so:
    <<set $now to new Date()>>
    

    Thirdly, I'd suggest setting all dates as UTC and using the UTC methods where applicable, so that all players see the same game world dates and times.

    Thrown wrote: »
    1) How would I add time manually to this one (+ 15min) or (8 hours) etc.
    <<run $now.setUTCMinutes($now.getUTCMinutes() + 15)>>
    
    <<run $now.setUTCHours($now.getUTCHours() + 8)>>
    

    Thrown wrote: »
    2) Or to add a set time per click?
    If you mean add a certain number of minutes, or something to that effect, each "turn", then you could do something like I showed above in a predisplay task. For example:
    predisplay["advanceTimeEveryTurn"] = function () {
    	var storyVars = variables();
    	storyVars["now"].setUTCMinutes(storyVars["now"].getUTCMinutes() + 15);
    };
    
    If you have passage where you don't want time to pass, you could test for the presence of a tag or tags and skip the time advancement based on that. For example:
    predisplay["advanceTimeEveryTurn"] = function () {
    	if (tags().contains("notime")) { return; }
    
    	var storyVars = variables();
    	storyVars["now"].setUTCMinutes(storyVars["now"].getUTCMinutes() + 15);
    };
    

    Thrown wrote: »
    3) I'd like to drop the GMT-0500 (Central Daylight Time) from the StoryCaption, but from what I read on the Java sites it seems more involved than its worth. Is there an easy way to not populate it? I know the T in "2009-05-08T08:00" is related to it, is there a letter like X or something to drop it?
    No, the T isn't related to it at all. It simply separates the calendar date from the time in the abbreviated ISO8601 date/time string used to construct the Date. It has absolutely no bearing on what's being printed.

    You're seeing the TZ info, because <<print $now>> is coercing the Date object in $now to a string, which calls its toString() method. If you don't want to see the TZ, then you have two choices: attempt to remove it from the returned formatted string before printing it or use another method to generate the full date string. I'd probably suggest the latter (as I do in the widget package I'm posting after this).
  • edited October 2015
    Mod note: TME asked that this note be added: "Please see this post for updated Gregorian Date & Time Widgets."


    I'm reposting these widgets here in the hopes someone will find them useful.

    The widgets use the JavaScript Date object to provide the inner workings of a Gregorian Calendar (a.k.a. Christian/Western calendar). The initial date should be set in UTC (just set it to the date/time you want, but note that it's in UTC), so that all players see the same game world dates and times (this is also the reason why all of the associated widgets use the UTC methods where applicable).

    Gregorian Date & Time Widgets: (goes in a widget tagged passage)
    /*
    	Date & Time Widget Setup
    */
    <<set
    	/* 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. */
    >>
    <<set
    	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])>>\
    <</widget>>
    
    /* Adds the specified number of hours. */
    <<widget "addhours">>\
    <<run $gameDate.setUTCHours($gameDate.getUTCHours() + $args[0])>>\
    <</widget>>
    
    /* Adds the specified number of days. */
    <<widget "adddays">>\
    <<run $gameDate.setUTCHours($gameDate.getUTCHours() + $args[0] * 24)>>\
    <</widget>>
    
    
    /*
    	Date & Time Printing Widget Definitions
    */
    /* Prints the current date ("{weekday} {month} {day}, {year}"). */
    <<widget "date">>\
    <<print String.format("{0} {1} {2}, {3}",
    	GameDays[$gameDate.getDay()],
    	GameMonths[$gameDate.getMonth()],
    	$gameDate.getDate(),
    	$gameDate.getFullYear()
    )>>\
    <</widget>>
    
    /* Prints the current time (12H). */
    <<widget "time12h">>\
    <<if $gameDate.getUTCHours() eq 0>>\
    12\
    <<elseif $gameDate.getUTCHours() gt 12>>\
    <<print $gameDate.getUTCHours() - 12>>\
    <<else>>\
    <<print $gameDate.getUTCHours()>>\
    <</if>>:\
    <<if $gameDate.getUTCMinutes() lt 10>>0<</if>><<print $gameDate.getUTCMinutes()>> \
    <<if $gameDate.getUTCHours() gte 12>>PM<<else>>AM<</if>>\
    <</widget>>
    
    /* 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()>>\
    <</widget>>
    
    /* Prints the current date and time (12H). */
    <<widget "datetime">><<date>> <<time12h>><</widget>>
    
    The initial game date/time is set in the setup section. Each of the printing widgets can be customized.

    Usage, Printing:
    ''Date:'' <<date>>
    ''Time:'' <<time12h>> (12H) / <<time24h>> (24H)
    ''Both:'' <<datetime>> (12H)
    
    Usage, Advancing:
    /* Adds 5 minutes. */
    <<addmins 5>>
    
    /* Adds 5 hours. */
    <<addhours 5>>
    
    /* Adds 5 days. */
    <<adddays 5>>
    
  • Oh, man! I tried all kinds of things like this:
    <<set $now to new Date(Date.now())>>
    and <<run $now.setUTCHours($now.getUTCHours())>>
    

    But I thought the minutes went in here:
    <<set $now to new Date(Date.now(15))>>
    
    Not here...
    <<set $now to new Date(Date.now()15)>>
    

    I'm finding the 'unspoken' things like that, the most difficult to deal with. It's so basic no one talks about it. I never would've thought it was the other way. My brain wants to treat it like math, when I know full well it isn't. I did read that the T / UTC changed the zones and was hoping for an easy fix like X for null.

    You're a saint. I REALLY did try to figure it out. I got one of the others to work and advance, but it didn't do days or months. I seriously wasted like 40 hours playing with all these different clocks, but I'm sure a ton of people are going to use this post in the future.

    Thanks again Mad, from all of us. I'm really enjoying the hell out of Twine as well as learning to code and all of the helpful people here are the reason why.

  • Thrown wrote: »
    Not here...
    <<set $now to new Date(Date.now()15)>>
    
    That's still not correct (I assume you're trying to set $now to the current date/time + 15 minutes). Your example is missing the addition operator and you cannot add the minutes like that. The Date.now() static method returns milliseconds, so to add 15 minutes to it you would have to match that (i.e. 15 minutes worth of milliseconds). For example:
    <<set $now to new Date(Date.now() + 15 * 60 * 1000)>>
    
    Or, using SugarCube's Util.fromCSSTime() method, which also returns milliseconds:
    <<set $now to new Date(Date.now() + Util.fromCSSTime("15m"))>>
    

    Alternatively, if you don't want to fiddle with milliseconds, you could simply set $now to the current time and then use the UTC minute get/set methods I showed above to add the 15 minutes. For example:
    <<set $now to new Date(); $now.setUTCMinutes($now.getUTCMinutes() + 15)>>
    
  • Sorry, I just grabbed that as an example for the brackets and math’s order of operations. I hadn't seen this anywhere-> () + 15)
    so I was experimenting for 2 weeks with this (+15)).

    So the advance time thing I have down.

    Oh crap! TAGGED widget, not named widget. :D I never even noticed the 'tag' thing before a second ago.

    That's the other piece. I knew I wasn't a moron and that I understood what I was looking at and how it should work. Knew it was something little. I really do think I'm getting this, but little things like that cost me 40 hrs of hair pulling. The whole time I was embarrassed to ask. I feel like we just rebuilt an engine only to have you tell me I was just out of gas, lol.

    You're a Saint man. I'll go through everything later and see whats there.
  • And the 'predisplay' is a passage titled and/or tagged predisplay?

  • Neither. It's the predisplay task object. It's JavaScript code, so it goes into the Story JavaScript (sorry, I should have mentioned that).
  • predisplay["advanceTimeEveryTurn"] = function () {
    	var storyVars = variables();
    	storyVars["now"].setUTCMinutes(storyVars["now"].getUTCMinutes() + 15);
    };
    

    No worries, I tried it there first since it ended in };
    It threw an error Cannot read property 'setUTCMinutes' of undefined.

    I just tried it again and it worked on the <<print $now>> clock, but not the Widget Clock; until I closed the opened a fresh game, then it threw the error again. (Not planning on using both clocks, just never removed the other one.)

    Does it need to be reworded to 'gameMinutes' 'nowMinutes' or something to run with the widgets?
    story ini time set:
    <<set $gameDate to new Date("2011-04-30T12:00z")>>
    
    Widget time
    	/* This must be set to whatever the initial game date/time should be. */
    	$gameDate to new Date("2011-04-30T12:00z"); /* Must use UTC time. */
    
  • You need to change it to use the same story $variable that the widgets use (i.e. $gameDate). For example:
    predisplay["advanceTimeEveryTurn"] = function () {
    	var storyVars = variables();
    	storyVars["gameDate"].setUTCMinutes(storyVars["gameDate"].getUTCMinutes() + 15);
    };
    
  • For anyone else that uses this, here is the code for: if a passage is only there during set times (in this case a store open from 8am-5pm). (24hr clock)
    <<if $gameDate.getUTCHours() lt 8, $gameDate.getUTCHours() gt 17>>We're Closed<<endif>>
    

    It seems to test fine, but if there is an issue (or a better way) please let me know. Took me awhile to figure it out, hope it saves someone some time.
  • You conjoin separate conditional expressions together via the logical AND (and) and OR (or) operators, not the comma (,) operator. Which logical operator you use depends on what the logic is supposed to be doing.


    In your example, there are two ways to look at the store's state:
    • Open: (time is greater than or equal to 8) and (time is less than or equal to 17).
    • Closed: (time is less than 8) or (time is greater than 17).

    For example:
    <<if $gameDate.getUTCHours() gte 8 and $gameDate.getUTCHours() lte 17>>Open<<else>>Closed<</if>>
    
    <<if $gameDate.getUTCHours() lt 8 or $gameDate.getUTCHours() gt 17>>Closed<<else>>Open<</if>>
    
  • Oh, okay, thanks! I thought I tried 'and', but I must have had something backwards. Just happy to get it that close and pretty quickly, where as I've been trying to figure out a simple one for awhile.

    What's the shortest way to do 'the alarm clock awakes you up at 8am no matter what time you go to bed (and roll the date forward)? I know there must be a super short way, but I can't hit on it. Resigned myself to doing it in 8-10 lines of
    <<if $gameDate.getUTCHours() lte 1>><<addhours 7>><<endif>>
    
    increasing the spread and add an hour each line. That's why I figured out the store thing above as a work around for this. If bedtime is between 4-5 add 3 hours. 5-6 add 2 hours, etc.

    What is the shorter path O Guru of Sugary Wisdom...
  • You are not forgotten. I'll try to get to this tomorrow.
  • Since you always want it to be exactly 8am, couldn't you just
    <<run $gameDate.setUTCMinutes(0)>>
    <<run $gameDate.setUTCHours(8)>>
    <<adddays 1>>
    

    instead of doing all kinds of fancy maths, or is there something I'm missing there? Seemed to work out when I tested it real quick.
  • edited August 2015
    No, but I know there's a simple way. When I did that, if it were 1am-7am it would jump forward 24+ hours. I just tried it again to make sure having it:
    <<run $gameDate.setUTCMinutes(0)>>
    <<run $gameDate.setUTCHours(8)>>
    <<adddays 1>>
    
    vs
    
    <<adddays 1>>
    <<run $gameDate.setUTCMinutes(0)>>
    <<run $gameDate.setUTCHours(8)>>
    

    made a difference- it doesn't. (In case I had it the 2nd way before).

    I guess I could work out a < or > statement that only adds the day if after midnight?
    That gets me closer, I think, Eddies. 2 lines of code instead of 10.

    Or I could have the player eaten by a Grue or turn into a Gremlin if they don't go to bed before midnight, lol. :D

    Thanks, I'll try it in a minute... <- snicker.

    Something like this (if it's not correct tell me). Tested and seems to work fine.
    <<if $gameDate.getUTCHours() lte 23 and $gameDate.getUTCHours() gte 18>>
    <<run $gameDate.setUTCMinutes(0)>>
    <<run $gameDate.setUTCHours(8)>>
    <<adddays 1>>
    <<elseif $gameDate.getUTCHours() gte 0 and $gameDate.getUTCHours() lte 8>>
    <<run $gameDate.setUTCMinutes(0)>>
    <<run $gameDate.setUTCHours(8)>><</if>>
    

    Okay I think that works now...

    One step closer to: The TwineCube Clock- The Definitive Edition.

    Anyone else using this need it to do anything else- just ask. I'm sure there are other time related issues I haven't need yet.

    -Thanks again Eddies!
  • -high five- glad I could give you a wrong answer that led to a right answer haha.
  • edited August 2015
    <<if GameMonths[$gameDate.getMonth()] is "Apr">>[img[Image Location]]<<endif>>
    
    That will throw the pic if the month is April, but I can't seem to nail the syntax for the date (4-30-2009). What is the variable for the 30th for the widgets (or 4-30)? "Day" is Mon etc and "Date" looks like a range including the time. Not sure if I can't see it because my eyes are burning or if it's the Devil's Cut... :) This for showing the daily Newspaper...
  • /* Either this. */
    <<if GameMonths[$gameDate.getMonth()] is "Apr" and $gameDate.getDate() is 30>>[img[Image Location]]<</if>>
    
    /* Or this. */
    <<if $gameDate.getMonth() is 3 and $gameDate.getDate() is 30>>[img[Image Location]]<</if>>
    
    The latter uses 3 for the 4th month because months are 0-based (i.e. Jan is 0, Feb is 1, etc).

    PS: I've also just realized that I setup the widgets backwards. They should use the local time methods, not the UTC methods, and the time should be specified without a timezone offset. I'll put out a fixed version soon.
  • That's the ticket, thanks Mad. I was confused about the UTC vs local thing, now I know why... :)
  • I'm reposting these widgets here in the hopes someone will find them useful.

    The widgets use the JavaScript Date object to provide the inner workings of a Gregorian Calendar (a.k.a. Christian/Western calendar).

    Set the initial date to the date/time you want and all players should see the same game world dates and times.

    Gregorian Date & Time Widgets: (goes in a widget tagged passage)
    /*
    	Date & Time Widget Setup
    */
    <<set
    	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"
    	];
    
    	/*
    		Below we have to use the multi-parameter version of the Date
    		constructor, rather than the date string version, because the
    		date string version treats a missing timezone offset as UTC.
    		While there are ways to determine players' timezone offsets,
    		so they could be added to a date string, it's more convenient
    		simply to use the multi-parameter constructor.
    
    		The point of this is so that you can simply initialize the game
    		world clock to whatever date and time you wish without having to
    		worry about the players' timezone offsets, while still ensuring
    		that they all see the same game world dates and times.
    	*/
    	/* params: year , month(0-based) , day , hour(24H) , minute [, second ] */
    	$gameDate to new Date(2015, 2, 17, 3, 24); /* e.g. Mar 17, 2015 03:24 */
    >>
    
    
    /*
    	Date & Time Advancement Widget Definitions
    */
    /* Adds the specified number of minutes. */
    <<widget "addmins">>\
    <<run $gameDate.setMinutes($gameDate.getMinutes() + $args[0])>>\
    <</widget>>
    
    /* Adds the specified number of hours. */
    <<widget "addhours">>\
    <<run $gameDate.setHours($gameDate.getHours() + $args[0])>>\
    <</widget>>
    
    /* Adds the specified number of days. */
    <<widget "adddays">>\
    <<run $gameDate.setHours($gameDate.getHours() + $args[0] * 24)>>\
    <</widget>>
    
    
    /*
    	Date & Time Printing Widget Definitions
    */
    /* Prints the current date ("{weekday} {month} {day}, {year}"). */
    <<widget "date">>\
    <<print String.format("{0} {1} {2}, {3}",
    	GameDays[$gameDate.getDay()],
    	GameMonths[$gameDate.getMonth()],
    	$gameDate.getDate(),
    	$gameDate.getFullYear()
    )>>\
    <</widget>>
    
    /* Prints the current time (12H). */
    <<widget "time12hr">>\
    <<if $gameDate.getHours() eq 0>>\
    12\
    <<elseif $gameDate.getHours() gt 12>>\
    <<print $gameDate.getHours() - 12>>\
    <<else>>\
    <<print $gameDate.getHours()>>\
    <</if>>:\
    <<if $gameDate.getMinutes() lt 10>>0<</if>><<print $gameDate.getMinutes()>> \
    <<if $gameDate.getHours() gte 12>>PM<<else>>AM<</if>>\
    <</widget>>
    
    /* Prints the current time (24H). */
    <<widget "time24hr">>\
    <<if $gameDate.getHours() lt 10>>0<</if>><<print $gameDate.getHours()>>:\
    <<if $gameDate.getMinutes() lt 10>>0<</if>><<print $gameDate.getMinutes()>>\
    <</widget>>
    
    /* Prints the current date and time (12H). */
    <<widget "datetime">><<date>> <<time12hr>> (<<time24hr>>)<</widget>>
    
    The initial game date/time is set in the setup section. Each of the printing widgets can be customized.

    Usage, Printing:
    ''Date:'' <<date>>
    ''Time:'' <<time12h>> (12H) / <<time24h>> (24H)
    ''Both:'' <<datetime>> (12H)
    
    Usage, Advancing:
    /* Adds 5 minutes. */
    <<addmins 5>>
    
    /* Adds 5 hours. */
    <<addhours 5>>
    
    /* Adds 5 days. */
    <<adddays 5>>
    
  • @Thrown: Note that you'll need to drop the UTC from all of the methods for stuff you're doing outside of the widgets.
  • thx for this awesome widget stuff, my appologize but there's small typo in printing usage
    ''Time:'' <<time12h>> (12H) / <<time24h>> (24H)
    
    it should
    <<time12hr>> and <<time24hr>>

    it should be works now
  • edited December 2015
    but why when i test the printing usage, just copy paste it and run in my localhost
    this is what i get
    Error: <<date>>: error within widget contents (Error: <<print>>: bad expression: Cannot read property 'getDay' of undefined)
    Time: Error: <<time24hr>>: errors within widget contents (Error: <<if>>: bad conditional expression in <<if>> clause: Cannot read property 'getHours' of undefined; Error: <<print>>: bad expression: Cannot read property 'getHours' of undefined; Error: <<if>>: bad conditional expression in <<if>> clause: Cannot read property 'getMinutes' of undefined; Error: <<print>>: bad expression: Cannot read property 'getMinutes' of undefined)
    Both: Error: <<datetime>>: errors within widget contents (Error: <<date>>: error within widget contents (Error: <<print>>: bad expression: Cannot read property 'getDay' of undefined); Error: <<time24hr>>: errors within widget contents (Error: <<if>>: bad conditional expression in <<if>> clause: Cannot read property 'getHours' of undefined; Error: <<print>>: bad expression: Cannot read property 'getHours' of undefined; Error: <<if>>: bad conditional expression in <<if>> clause: Cannot read property 'getMinutes' of undefined; Error: <<print>>: bad expression: Cannot read property 'getMinutes' of undefined))
    

    but when run in twine 2.0 app , the codes are running ok ... is it bug or i'm missing something here ?

    PS: I paste Gregorian Date & Time Widgets in a widget tagged passage named widget, and the printing usage in just ordinary passage
  • WOOOPSSS , im sorry this time is my bad.. my browser have cache from previous editing but after click RESTART in my game, the code working fine, the last one is just reloading the browser so the cache is still there so the code is error LOL
  • This was a silly mistake on my end, but when setting the initial datetime remember not to use a leading 0 as it will be interpreted as an octal literal.

    Whoops!
  • Hi,

    First, I'm very sorry to dig up an old thread.
    I found that widget (see below) and It seems that I miss something.
    I use Twine 2 and sugarcube 2 (I'm also begin in javascript).

    I just copy it. Put it in a "passage" tagged "widget".
    In a side bar, I add with a CSS code, I want to put in it the date.
    So I write in it :
    ''Date:'' $gameDate

    and
    <<set $gameDate to new Date('February 1, 2017 08:00')>>
    in the StoryInit

    So it shows : "Date: 1/2/2017 08:00:00" and not "Date: Feb 1, 2017 08:00"

    Is there a possibility to have the second display ?

    Thx.


    I'm reposting these widgets here in the hopes someone will find them useful.

    The widgets use the JavaScript Date object to provide the inner workings of a Gregorian Calendar (a.k.a. Christian/Western calendar).

    Set the initial date to the date/time you want and all players should see the same game world dates and times.

    Gregorian Date & Time Widgets: (goes in a widget tagged passage)
    /*
    	Date & Time Widget Setup
    */
    <<set
    	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"
    	];
    
    	/*
    		Below we have to use the multi-parameter version of the Date
    		constructor, rather than the date string version, because the
    		date string version treats a missing timezone offset as UTC.
    		While there are ways to determine players' timezone offsets,
    		so they could be added to a date string, it's more convenient
    		simply to use the multi-parameter constructor.
    
    		The point of this is so that you can simply initialize the game
    		world clock to whatever date and time you wish without having to
    		worry about the players' timezone offsets, while still ensuring
    		that they all see the same game world dates and times.
    	*/
    	/* params: year , month(0-based) , day , hour(24H) , minute [, second ] */
    	$gameDate to new Date(2015, 2, 17, 3, 24); /* e.g. Mar 17, 2015 03:24 */
    >>
    
    
    /*
    	Date & Time Advancement Widget Definitions
    */
    /* Adds the specified number of minutes. */
    <<widget "addmins">>\
    <<run $gameDate.setMinutes($gameDate.getMinutes() + $args[0])>>\
    <</widget>>
    
    /* Adds the specified number of hours. */
    <<widget "addhours">>\
    <<run $gameDate.setHours($gameDate.getHours() + $args[0])>>\
    <</widget>>
    
    /* Adds the specified number of days. */
    <<widget "adddays">>\
    <<run $gameDate.setHours($gameDate.getHours() + $args[0] * 24)>>\
    <</widget>>
    
    
    /*
    	Date & Time Printing Widget Definitions
    */
    /* Prints the current date ("{weekday} {month} {day}, {year}"). */
    <<widget "date">>\
    <<print String.format("{0} {1} {2}, {3}",
    	GameDays[$gameDate.getDay()],
    	GameMonths[$gameDate.getMonth()],
    	$gameDate.getDate(),
    	$gameDate.getFullYear()
    )>>\
    <</widget>>
    
    /* Prints the current time (12H). */
    <<widget "time12hr">>\
    <<if $gameDate.getHours() eq 0>>\
    12\
    <<elseif $gameDate.getHours() gt 12>>\
    <<print $gameDate.getHours() - 12>>\
    <<else>>\
    <<print $gameDate.getHours()>>\
    <</if>>:\
    <<if $gameDate.getMinutes() lt 10>>0<</if>><<print $gameDate.getMinutes()>> \
    <<if $gameDate.getHours() gte 12>>PM<<else>>AM<</if>>\
    <</widget>>
    
    /* Prints the current time (24H). */
    <<widget "time24hr">>\
    <<if $gameDate.getHours() lt 10>>0<</if>><<print $gameDate.getHours()>>:\
    <<if $gameDate.getMinutes() lt 10>>0<</if>><<print $gameDate.getMinutes()>>\
    <</widget>>
    
    /* Prints the current date and time (12H). */
    <<widget "datetime">><<date>> <<time12hr>> (<<time24hr>>)<</widget>>
    
    The initial game date/time is set in the setup section. Each of the printing widgets can be customized.

    Usage, Printing:
    ''Date:'' <<date>>
    ''Time:'' <<time12h>> (12H) / <<time24h>> (24H)
    ''Both:'' <<datetime>> (12H)
    
    Usage, Advancing:
    /* Adds 5 minutes. */
    <<addmins 5>>
    
    /* Adds 5 hours. */
    <<addhours 5>>
    
    /* Adds 5 days. */
    <<adddays 5>>
    

  • Alianna wrote: »
    Is there a possibility to have the second display?
    There are two issues with your example code:

    1. The format of the value you passing to the new Date object you are assigning to the $gameDate variable.

    The instructions included in the widget's source code warn about the UTC timezone issue when using a String to initialise the date. Your initialisation should look like the following and ideally you should be modifying the default value set inside the widget code.
    eg. replace the $gameDate to new Date(2015, 2, 17, 3, 24); line in the code with.
    $gameDate to new Date(2017, 1, 1, 8, 0);
    
    ... to have a date of February 1, 2017 08:00.


    2. You are not using the included widgets to display the date and time.

    The code includes four widgets/macros (date, time12hr, time24hr, datetime) which should be used to display the game date. Try using the following to achieve the output you want.
    Date: <<date>> <<time24hr>>
    
  • Hi, thanks for the answer.

    So I modify the code (for the date) in the passage (I name it calendar and tag it with "widget")

    I write
    Date: <<date>> <<time24hr>>
    in my new sidebar (explain by you BTW ^^) : http://twinery.org/forum/discussion/5764/sugarcube-apply-html-code-on-every-passage

    it says :
    Date: Error: macro <<date>> does not exist Error: macro <<time24hr>> does not exist

    So I think I miss something.
  • Alianna wrote: »
    So I think I miss something.
    Did you follow the instructions in this comment by TheMadExile and add his code to a widget tagged passage?
    You need to do that so that those macros are available in your story and so you can use them like I did in my examples
  • edited January 2017
    Hi,


    yes I do it (see pics attached)

    English isn't my native language, so I could misunderstand a sentence. But I think it's good.

    Thx for your time.
Sign In or Register to comment.