public
Created

RelativeDate Node.js module

  • Download Gist
index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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;
};
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
{
"author": "Jan Kuča <jan@jankuca.com> (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": "*"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.