0 votes
by (710 points)
edited by
$CurDate and $Appointment print in this format: Thursday, 6/14/2018, 3:30:00 PM. Is there any way to print without seconds so that it reads: Thursday, 6/14/2018, 3:30PM ?

Also, I am having trouble comparing $Appointment and $CurDate in an If statement.

<<if $Appointment == $CurDate>>
[[Go to Appointment|Appointment]]
<</if>>

This code doesn't seem to work since the link never shows up in game even when I increment $CurDate so that it equals $Appointment.

 

Thank you for your answer to my previous question!

2 Answers

+1 vote
by (44.7k points)
selected by
 
Best answer

If you don't like the default display of Date objects, you can use the Date function ".toLocaleString()" to display it however you want.  For what you're looking for you'd do this:

<<set _timeOptions = { weekday: 'long', year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric' }>>
Formatted date: <<print $Appointment.toLocaleString("en-US", _timeOptions)>>

See the link above for an explanation of the options.

As for comparing the dates, "<<if $Appointment == $CurDate>>" won't work because these are objects, and objects aren't equal unless they have the same reference, even if they are otherwise identical in value.

If the dates must match, but the times are irrelevant, then you could use this:

<<if $Appointment.toDateString() == $CurDate.toDateString()>>

If instead you want to figure out if your appointment is within the next half an hour, already passed, or over 30 minutes away, then you could use this:

<<if ($Appointment - $CurDate >= 0) && ($Appointment - $CurDate < 30*60*1000)>>
	Time to go!
<<elseif ($Appointment - $CurDate < 0)>>
	Too late.
<<else>>
	You have plenty of time.
<</if>>

When you subtract one Date object from another, the result is the time difference in milliseconds.

The "30*60*1000" is "30 minutes × 60 seconds × 1000 milliseconds", which translates to "30 minutes worth of milliseconds" or 1800000.  You can use that math if you need to represent other time differences.

Hope that helps! smiley

by (710 points)
Thank you for the answer. You even anticipated my next question (figuring out if the appointment is within half an hour)!
+1 vote
by (159k points)

Please use the Question Tags to state the name and full version number of the Story Format you are using, as answers can vary based on that information. Based on the syntax of your example I will assume you are using v2.21.0 of SugarCube. Also please use the Insert Code Snippet button when adding a code example to your posts.

You don't state this but based on your questions I will assume that your $CurDate and $Appointment story variables contain JavaScript Date objects.

An object is similar to a box with one or more things inside it, in the case of a Date object that box generally contains a large number that represents the number of milliseconds since a specific point in time in the past.

So in your comparison example you are trying to compare two different boxes, which obviously can never be the same because they are not the same box. So you need to compare the contents of the boxes instead, which in the case of the Date objects is the number of milliseconds returned by the <date>.getTime() function.

<<if $Appointment.getTime() == $CurDate.getTime()>>
[[Go to Appointment|Appointment]]
<</if>>

 

by (710 points)
Thank you for the answer! I tried to take a code snippet for my original thread, but I can't find the button. It is supposed to be "the first button on the left of the toolbar,"  but on my toolbar that is the home button. I am using ubuntu if that helps.
...