For the fun of it, I created an interactive decision tree for playing the game of Guess Who?.
Try it out and let me know if you come up with better features that create a smaller decision tree.
Originally I had more features, but removed them because I decided that if I had to explain to my 7 year old what I meant, it probably wasn’t a fair question.
For the impatient, scroll to the bottom of the page for an interactive example.
Date.fromString() is a method that allows you to easily parse user input into a date. There are currently many implementations of calendar pop-ups that aim to solve a similar problem. However, these calendars are cumbersome to use if the target date is more than a couple months away. The goal of this project is to parse free-form user input into a valid Date object.
Date.fromString() accepts two parameters:
input string — User input string to parse
optionsoptional object — An object that can override the default behavior { order: 'MDY', strict: false }
When part of the date is left off, it fills in the missing parts with today’s details. When these examples were written, it was November 25, 2008.
console.log(Date.fromString('Feb 13th'));// Wed Feb 13 2008 00:00:00 GMT-0700 (MST)
console.log(Date.fromString('2/13'));// Wed Feb 13 2008 00:00:00 GMT-0700 (MST)
console.log(Date.fromString('4pm'))// Tue Nov 25 2008 16:00:00 GMT-0700 (MST)
If you would rather have the date not fill in those parts, pass the option strict set to true.
console.log(Date.fromString('4pm'))// Tue Nov 25 2008 16:00:00 GMT-0700 (MST)
console.log(Date.fromString('4pm',{strict:true}))// Invalid Date
Sometimes the date cannot be parsed without ambiguity. In these cases, the default ordering of month/day/year (MDY) is used. To change this behavior, pass the option order set to your preferred ordering; YMD, DMY, etc.
Finally, try it for yourself. I have not been able to test this much in browsers other than Firefox on Linux, so if you find any bugs, please let me know.
Recent Comments