Easy Date parsing with JavaScript
Posted: November 25th, 2008 | Author: Joey | Filed under: Coding | Tags: date, javascript, php, strtotime | 3 Comments »Date.fromString()
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 - options optional
object — An object that can override the default behavior
{ order: ‘MDY’, strict: false }
Examples
console.log(Date.fromString('Sept 5th, 2006 4:00 pm')); // Tue Sep 05 2006 16:00:00 GMT-0600 (MDT) console.log(Date.fromString('Sept 5th, 2006 4:23pm')); // Tue Sep 05 2006 16:23:00 GMT-0600 (MDT) console.log(Date.fromString('Sept 5th, 2006 4pm')); // Tue Sep 05 2006 16:00:00 GMT-0600 (MDT) console.log(Date.fromString('Sept 5th, 2006 12:34:56 am')); // Tue Sep 05 2006 00:34:56 GMT-0600 (MDT) console.log(Date.fromString('Sept 5th, 2006 7:30 am')); // Tue Sep 05 2006 07:30:00 GMT-0600 (MDT) console.log(Date.fromString('2006/09/05 4:23pm')); // Tue Sep 05 2006 16:23:00 GMT-0600 (MDT) console.log(Date.fromString('2006/09/05')); // Tue Sep 05 2006 00:00:00 GMT-0600 (MDT) console.log(Date.fromString('9/5/06')); //Tue Sep 05 2006 00:00:00 GMT-0600 (MDT) console.log(Date.fromString('2/13/78')); // Mon Feb 13 1978 00:00:00 GMT-0700 (MST) console.log(Date.fromString('Feb 13 09')); // Fri Feb 13 2009 00:00:00 GMT-0700 (MST)
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.
console.log(Date.fromString('09/05/06')); // Tue Sep 05 2006 00:00:00 GMT-0600 (MDT) console.log(Date.fromString('09/05/06', {order: 'YMD'})); // Wed May 06 2009 00:00:00 GMT-0600 (MDT) console.log(Date.fromString('09/05/06', {order: 'DMY'})); // Tue May 09 2006 00:00:00 GMT-0600 (MDT)
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.
Output:
Downloaded a total of 84 times
Javascript Date.fromString() (5.27 KB)

Recent Comments