UNPKG

@seedalpha/moment

Version:

Extracted version of Momentjs

68 lines (62 loc) 1.59 kB
/* * Seed Alpha's momentjs implementation */ var extend = require('@seedalpha/extend'); var defaults = { _date: new Date(), _localeDateOptions: { weekday: "short", year: "numeric", month: "short", day: "numeric" } }; var checkISOString = function(ISOString) { if (ISOString.length == 24 && !isNaN(new Date(ISOString).getTime())) { return { _date: new Date(ISOString) }; } else { return; } } /** * [SeedMoment Constructor] */ var SeedMoment = function(options) { if (typeof options === 'string') { options = checkISOString(options) || { _date: new Date() }; } this._options = extend({}, defaults, options); this._date = this._options._date; } /** * [SeedMoment Prototype] */ SeedMoment.prototype = { getRelativeTime: function() { var ms = new Date().getTime() - this._date.getTime(); var sec = Math.floor(ms / 1000); var min = Math.floor(sec / 60); var hr = Math.floor(min / 60); var day = Math.floor(hr / 24); if (day > 6) { return this._date.toLocaleDateString('en', this._options._localeDateOptions); } else if (hr > 47) { return day + ' days ago'; } else if (hr > 23) { return 'a day ago'; } else if (min > 90) { return hr + ' hours ago'; } else if (min > 45) { return 'an hour ago'; } else if (sec > 90) { return min + ' minutes ago'; } else if (sec > 45) { return 'a minute ago'; } else if (sec > 10) { return sec + ' seconds ago'; } else { return 'just now'; } } } exports = module.exports = SeedMoment;