Skip to content

@jankuca /index.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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 <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": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.