API Docs for: 2.5.3
Show:

File: src/Helpers/date.js

/**
	Extension of 'Date' class
	
	@class Date
**/

/**
	Converts a numerical index to its corresponsing abbreviated string representation.

		var a = Date.getMonthName(0);
		var b = Date.getMonthName(1);
		var c = Date.getMonthName(13);

		// a ==> 'Jan'
		// b ==> 'Feb'
		// c ==> 'Feb'

	@method getMonthName
	@for Date

	@param {Number} month Represents a month on a zero-indexed, 12-month calendar.
	
	@return {String} Abbreviated month name
**/
Date.getMonthName = function(month) {
	var shortcut = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

	return shortcut[month % 12];
};

/**
	Converts a date data to `time-ago` string format.

		var a = Date.getTimeAgoFormat(new Date());
		var b = Date.getTimeAgoFormat(1474040255235);
		var c = Date.getTimeAgoFormat(new Date(), 'zh');
		var d = Date.getTimeAgoFormat(1474040255235, 'zh');
		var e = Date.getTimeAgoFormat(1474040255235, 'custom', {
			useBlank: true,
			timeString: {
				now: 'nowC',
				suffix: 'agoC'
			},
			timeFormats: [
				[60, 'secondsC', 1], // 60
				[3600, 'minutesC', 60], // 60*60, 60
				[86400, 'hoursC', 3600], // 60*60*24, 60*60
				[604800, 'daysC', 86400], // 60*60*24*7, 60*60*24
				[2419200, 'weeksC', 604800], // 60*60*24*7*4, 60*60*24*7
				[29030400, 'monthsC', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4
				[2903040000, 'yearsC', 29030400] // 60*60*24*7*4*12*100, 60*60*24*7*4*12
			]
		});

		// a ==> 'now'
		// b ==> '1 months ago'
		// c ==> '现在'
		// d ==> '1个月前'
		// e ==> '1 monthsC agoC'

	@method getTimeAgoFormat
	@for Date

	@param {Date} date data.
	@param {String} [options] langCode. (ko/en/zh, en: default code)
	@param {Object} [options] customTimeData if langCode == `custom`, use custom time data.
	
	@return {String} time-ago string format
**/
Date.getTimeAgoFormat = function(d, langCode, customTimeData) {
	if (!d) return '-';

	var LANG_SET = {
		'ko' : {
			useBlank: true,
			timeString: {
				now: '현재',
				suffix: '전'
			},
			timeFormats: [
				[60, '초', 1], // 60
				[3600, '분', 60], // 60*60, 60
				[86400, '시간', 3600], // 60*60*24, 60*60
				[604800, '일', 86400], // 60*60*24*7, 60*60*24
				[2419200, '주', 604800], // 60*60*24*7*4, 60*60*24*7
				[29030400, '달', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4
				[2903040000, '년', 29030400] // 60*60*24*7*4*12*100, 60*60*24*7*4*12
			]
		},
		'en' : {
			useBlank: true,
			timeString: {
				now: 'now',
				suffix: 'ago'
			},
			timeFormats: [
				[2, 'second', 1],
				[60, 'seconds', 1], // 60
				[120, 'minute', 60], // 60*2, 60
				[3600, 'minutes', 60], // 60*60, 60*2
				[7200, 'hour', 3600], // 60*60*2, 60*60
				[86400, 'hours', 3600], // 60*60*24, 60*60*2
				[172800, 'day', 86400], // 60*60*24*2, 60*60*24
				[604800, 'days', 86400], // 60*60*24*7, 60*60*24*2
				[1209600, 'week', 604800], // 60*60*24*7*2, 60*60*24*7
				[2419200, 'weeks', 604800], // 60*60*24*7*4, 60*60*24*7*2
				[4838400, 'month', 2419200], // 60*60*24*7*4*2, 60*60*24*7*4
				[29030400, 'months', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4*2
				[58060800, 'year', 29030400], // 60*60*24*7*4*12*2, 60*60*24*7*4*12
				[2903040000, 'years', 29030400] // 60*60*24*7*4*12*100, 60*60*24*7*4*12*2
			]
		},
		'zh' : {
			useBlank: false,
			timeString: {
				now: '现在',
				suffix: '前'
			},
			timeFormats: [
				[60, '秒', 1], // 60
				[3600, '分钟', 60], // 60*60, 60
				[86400, '小时', 3600], // 60*60*24, 60*60
				[604800, '天', 86400], // 60*60*24*7, 60*60*24
				[2419200, '周', 604800], // 60*60*24*7*4, 60*60*24*7
				[29030400, '个月', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4
				[2903040000, '年', 29030400] // 60*60*24*7*4*12*100, 60*60*24*7*4*12
			]
		}
	};

	var timeData = langCode ? langCode === 'custom' ? customTimeData : LANG_SET[langCode] || LANG_SET['en'] : LANG_SET['en'],
		blankString, timeFormats;

	blankString = timeData.useBlank ? ' ' : '';
	timeFormats = timeData.timeFormats;

	var time = new Date(d),
		seconds = (+new Date() - time) / 1000;

	if (Math.floor(seconds) == 0) {
		return timeData.timeString.now;
	}

	var i = 0, format, list_choice = 1;

	while ((format = timeFormats[i++]))
		if (seconds < format[0]) {
			if (typeof format[2] == 'string')
				return format[list_choice];
			else
				return Math.floor(seconds / format[2]) + blankString + format[1] + blankString + timeData.timeString.suffix;
		}
	return time;
};