Populating a date value into an array

Twine Version: 2.3.9
Story Format: 2.33.2

I’ve got three NPCs (mom, dad, sibling) and each has an array I was using to track their details. Except for the schedule dates everything is working nicely.

What I need to do is to push a date into the schedule start/end values. The string has a padded date, as well as a fixed start/end time. Each has about 8-12 locations depending on if it’s a weekday, weekend, or holiday.

A snippet of the sibling array is below (shortened the schedule to keep it small). This is initialized as part of the StoryInit and are completed in the first few passages.

<<set $sibling to {
	name: '',
	gender: '',
	age: 20,
	relationship: 0,
	schedule: {
		weekday: {
			gym: {
				start: '07:30 AM',
				end: '09:00 AM'
			}
		}
}>>

While the above works, the main problem is that the times are strings. Which means I have to convert the in-game time into a string to compare them. I’d like to have an proper datestamp to just do a straight .getTime() comparison. I’ve got it working for other simpler components, but having issues here. As I don’t care about the date, I’ve padded it with ‘January 1, 2000’. The times are fixed and should be the same across any playthrough.

I have tried:

<<set $sibling to {
	schedule: {
		weekday: {
			gym: {
				start: 'new Date('January 1, 2000, 07:30 AM')',
				end:  'new Date('January 1, 2000, 09:00 AM')'
			}
         }
    }
}>>

I knew this one would fail due to the nested single quotes. It threw up all sorts of red errors. So I tried:

<<set $sibling to {
	schedule: {
		weekday: {
			gym: {
				start: "new Date('January 1, 2000, 07:30 AM')",
				end: "new Date('January 1, 2000, 09:00 AM')"
			}
         }
    }
}>>

and

<<set $sibling to {
	schedule: {
		weekday: {
			gym: {
				start: (new Date('January 1, 2000, 07:30 AM'))",
				end: (new Date('January 1, 2000, 09:00 AM'))"
			}
         }
    }
}>>

Both just inserted the literal value including the “newDate” part.

It has to be something simple/stupid I’ve missed… This may also be severe overkill, if so feel free to say so and I can look at other options. :slight_smile:

Aside from the simple calendar in the house, I will also be using this when the player visits locations the three NPCs can travel to as well… about 5-6 locations. I don’t suspect there will be more than 3, the rest will be random chances of running into other NPCs.

Thanks in advance!

According to MDN the proper way to use the “Date() constructor” is

new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])

Passing time as a string is discouraged.

hours is 0 - 23

So I believe what you want is:

<<set $sibling to {
	schedule: {
		weekday: {
			gym: {
				start: new Date(2000, 0, 1, 7, 30),
				end:  new Date(2000, 0, 1, 9)
			}
         }
    }
}>>

2000, 0, 1 being January 1, 2000

I hope this works out for you.

That was exactly it. I was only looking at the Date Object on MDN, not the Constructor page.

Thanks!