Skip to content

Commit

Permalink
externalize date helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Dec 7, 2014
1 parent ad1fa1c commit b634004
Showing 1 changed file with 8 additions and 50 deletions.
58 changes: 8 additions & 50 deletions lib/helpers/dates.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,20 @@
'use strict';
var Utils = require('../utils/utils');
var Dates = require('../utils/dates');

/**
* Port of formatDate-js library (http://bit.ly/18eo2xw)
* Expose `moment` helper
*/

exports.formatDate = function(date, format) {
date = new Date(date);
return Dates.format(date, format);
};

exports.now = function(format) {
var date = new Date();
if (Utils.isUndefined(format)) {
return date;
} else {
return Dates.format(date, format);
}
};
exports.moment = require('helper-moment');

/**
* Modified version of http://bit.ly/18WwJYf
* Expose `dateformat` helper
*/

exports.timeago = function(date) {
date = new Date(date);

var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);

if (interval > 1) {
return interval + ' years ago';
}

interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + ' months ago';
}
exports.dateformat = require('helper-dateformat');

interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + ' days ago';
}

interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + ' hours ago';
}

interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + ' minutes ago';
}
/**
* Expose `date` helper
*/

if (Math.floor(seconds) === 0) {
return 'Just now';
} else {
return Math.floor(seconds) + ' seconds ago';
}
};
exports.date = require('helper-date');

0 comments on commit b634004

Please sign in to comment.