Howdy, Stranger!

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

Questions about the (current-time:) macro.

I want to use the Harlowe (current-time:) macro to display the time, but it shows times like 5:09 AM as "5:9 AM." How do I add that leading zero back in? And is there any way to include the seconds as well? I tried making a clock in Javascript, but a clock that works in my HTML file doesn't work in a Twine file (supposedly because the stylesheet isn't at the end of the document, according to the internet.)

Comments

  • The Javascript code for Harlowe's (current-time:) macro works similar to following:
    currenttime = function() {
    	const d = new Date(), am = d.getHours() < 12;		
    	return d.getHours() % 12 + ":" + d.getMinutes() + " " + (am ? "A" : "P") + "M";
    }
    
    ... it does not support zero padding the minutes value, not does it include displaying seconds.

    You can create your own currentTime function with the features you want.

    warning: The following uses a Javascript substr function with a negative start index which may not be supported by all web-browser so you may want to add the following Javascript Polyfill (from the above MDN link) to the start of your Story Javascript area. The Story Javascript area is access via the Edit Story Javascript menu item.
    // only run when the substr() function is broken
    if ('ab'.substr(-1) != 'b') {
    	/**
       *  Get the substring of a string
       *  @param  {integer}  start   where to start the substring
       *  @param  {integer}  length  how many characters to return
       *  @return {string}
       */
    	String.prototype.substr = function(substr) {
    		return function(start, length) {
    			// call the original method
    			return substr.call(this,
    				// did we get a negative start, calculate how much it is from the beginning of the string adjust the start parameter for negative value
    				start < 0 ? this.length + start : start,
    				length)
    		}
    	}(String.prototype.substr);
    }
    

    The following Javascript code creates a My.currentTime() function which returns the current time (hours:minutes:seconds), with both the minutes and seconds being zero padded as needed. The function is contained within a namespace named My so that the function does not interfere with Harlowe's or the web-browser's code, you can change the name of the namespace to whatever you prefer by just changing all occurrences of My.
    The code needs to be placed within the Story Javascript area (after the above Polyfill if you also added it)
    if (! window.My) {
    	window.My = {
    		currentTime: function() {
    			const d = new Date(), am = d.getHours() < 12;
    			const m = "0" + d.getMinutes(), s = "0" + d.getSeconds();
    			
    			return d.getHours() % 12 + ":" + m.substr(-2) + ":" + s.substr(-2) + " " + (am ? "A" : "P") + "M";
    		}
    	};
    }
    

    You use a (print:) macro like the following to display the current time in a passage:
    time: (print: My.currentTime())
    
  • Thanks so much! That's exactly what I needed to know. :)
  • I put this code into my story, but I'm getting an "Unexpected Identifier" error. What could be causing this?
  • Did you cut-n-paste the code example that starts with "// only run when the substr() function is broken" and the code example that starts with "if (! window.My) {" into the Story Javascript area of your Story?

    The Story Javascript area is accessed via the Edit Story Javascript menu item.
  • I did. It might be an issue with the My window, because even when I trim it down to
    if (! window.My) {
    	window.My = {
    		currentTime: function() {
    			return "test";
    		}
    	};
    }
    
    it returns the Unexpected Identifier error.
  • The attached Archive HTML contains a demo of the above code, it has been tested on Windows 10 using Firefox, Chrome, IE and Edge. Download the archive HTML file and use Twine's Import From File option to add the demo to your Story List.

    I had to modify the currentTime function to the following:
    if (! window.My) {
    	window.My = {
    		currentTime: function() {
    			const d = new Date(), am = d.getHours() < 12;
    			const h = d.getHours() % 12, m = "0" + d.getMinutes(), s = "0" + d.getSeconds();
    			
    			return (h ? h : 12) + ":" + m.substr(-2) + ":" + s.substr(-2) + " " + (am ? "A" : "P") + "M";
    		}
    	};
    }
    
    ... because I incorrectly assumed that the original Harlowe 12-hour am/pm related code was valid but it displays One minute past midday/midnight as 0:01 when it should be 12:01.
Sign In or Register to comment.