Skip to content

Instantly share code, notes, and snippets.

@fiznool
Last active October 5, 2017 16:19
Show Gist options
  • Save fiznool/507e4d1b3da5fcc824f7 to your computer and use it in GitHub Desktop.
Save fiznool/507e4d1b3da5fcc824f7 to your computer and use it in GitHub Desktop.
Testing some date parsers
// Testing out some date parsing libraries.
'use strict';
var chrono = require('chrono-node');
require('datejs');
require('sugar');
var stringsToParse = [
'today',
'tomorrow',
'next week',
'next month',
'next year',
'11pm',
'11:30pm',
'23:30',
'Friday, 9th January 2015',
'Monday, 9th January 2015',
'friday at 10am',
'this friday at 10am',
'next friday at 10am',
'may 25th of next year',
'2014-01-18 09:30:00',
'2014-01-18 09:30:00 GMT',
'2014-01-18 09:30:00 PST',
'2014-01-18 09:30:00 -0400'
];
var dateDifferentials = [
'+1m',
'+1min',
'+1minute',
'+2m',
'+2mins',
'+2minutes',
'+1h',
'+1hr',
'+1hour',
'+2h',
'+2hrs',
'+2hours',
'+1d',
'+1day',
'+2d',
'+2days'
];
function pad(str) {
if(!str) {
str = '(null)';
}
str = str.toString();
for (var i = str.length; i < 39; i++) {
str = str + ' ';
}
return str;
}
console.log('Regular Parsing');
stringsToParse.forEach(function(str) {
var now = new Date();
console.log('');
console.log('Time now: ' + now);
console.log('To parse: ' + str);
console.log('|--------------------------------------------------|');
console.log('| DateJS: ' + pad(Date.parse(str)) + ' |');
console.log('| SugarJS: ' + pad(Date.future(str)) + ' |');
console.log('| Chrono: ' + pad(chrono.parseDate(str, now)) + ' |');
console.log('|--------------------------------------------------|');
});
console.log('');
console.log('');
console.log('Date Differential Parsing');
stringsToParse.forEach(function(str) {
var now = new Date();
dateDifferentials.forEach(function(dd) {
console.log('');
console.log('Time now: ' + now);
console.log('To parse: ' + str + ' ' + dd);
console.log('|--------------------------------------------------|');
console.log('| DateJS: ' + pad(Date.parse(str)) + ' |');
console.log('| SugarJS: ' + pad(Date.future(str)) + ' |');
console.log('| Chrono: ' + pad(chrono.parseDate(str, now)) + ' |');
console.log('|--------------------------------------------------|');
});
});
{
"name": "date-parsing-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chrono-node": "^1.0.2",
"datejs": "^1.0.0-rc3",
"sugar": "^1.4.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment