I have recently been playing with node.js. If you haven’t read about it yet, I recommend you do and that you try it out. I wrote a small app using it and hopefully I’ll get around to writing some articles about that application. In the meantime, I’m releasing a library I wrote for parsing command line options in javascript. This library will work with node.js.
The repository at bitbucket contains examples, so I’m not going to write much about the library here except to say to go here and get it! Let me know if you find it useful and post any bugs you find to the issue tracker.
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