Skip to content

Instantly share code, notes, and snippets.

@jankuca
Created January 20, 2011 16:21
Show Gist options
  • Save jankuca/788114 to your computer and use it in GitHub Desktop.
Save jankuca/788114 to your computer and use it in GitHub Desktop.
RelativeDate Node.js module
exports.parse = function (string, format) {
var match = string.match(/^(\+|-)\s*(\d+)\s*([a-z]{3})/i);
var add;
if (!match) {
add = 0;
} else {
add = parseInt(match[2], 10);
switch (match[3]) {
case 'min':
add *= 60;
break;
case 'hou':
add *= 3600;
break;
case 'day':
add *= 3600 * 24;
break;
case 'wee':
add *= 3600 * 24 * 7;
break;
case 'mon':
add *= 3600 * 24 * 30;
break;
case 'yea':
add *= 3600 * 24 * 365;
break;
}
if (match[1] === '-') {
add = -add;
}
}
if (format) {
var date = new Date();
var now = date.getTime();
date.setTime(now + add * 1000);
return date[format]();
}
return add;
};
{
"author": "Jan Kuča <[email protected]> (http://jankuca.com)",
"name": "relative-datetime",
"description": "Relative Date converter, + 1 year -> seconds",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "https://gist.github.com/788114"
},
"main": "index.js",
"dependencies": {},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment