rename vars; more efficient version of Ox.formatDate
This commit is contained in:
parent
5b8f1dd3b5
commit
a37949094d
1 changed files with 157 additions and 74 deletions
|
@ -8,12 +8,12 @@ Ox.formatArea <f> Formats a number of meters as square meters or kilometers
|
|||
'1 km\u00B2'
|
||||
@*/
|
||||
|
||||
Ox.formatArea = function(num, dec) {
|
||||
dec = Ox.isUndefined(dec) ? 8 : dec;
|
||||
var km = num >= 1000000;
|
||||
Ox.formatArea = function(number, decimals) {
|
||||
var k = number >= 1000000 ? 'k' : '';
|
||||
decimals = Ox.isUndefined(decimals) ? 8 : decimals;
|
||||
return Ox.formatNumber(
|
||||
(km ? num / 1000000 : num).toPrecision(dec)
|
||||
) + ' ' + (km ? 'k' : '') + 'm\u00B2';
|
||||
(k ? number / 1000000 : number).toPrecision(decimals)
|
||||
) + ' ' + k + 'm\u00B2';
|
||||
};
|
||||
|
||||
/*@
|
||||
|
@ -21,9 +21,8 @@ Ox.formatCurrency <f> Formats a number with a currency symbol
|
|||
> Ox.formatCurrency(1000, '$', 2)
|
||||
'$1,000.00'
|
||||
@*/
|
||||
|
||||
Ox.formatCurrency = function(num, str, dec) {
|
||||
return str + Ox.formatNumber(num, dec);
|
||||
Ox.formatCurrency = function(number, string, decimals) {
|
||||
return string + Ox.formatNumber(number, decimals);
|
||||
};
|
||||
|
||||
/*@
|
||||
|
@ -32,10 +31,10 @@ Ox.formatDate <f> Formats a date according to a format string
|
|||
<a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/strftime.3.html">strftime</a>
|
||||
and <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a>.
|
||||
'%Q' (quarter) and '%X'/'%x' (year with 'BC'/'AD') are non-standard
|
||||
(str) -> <s> formatted date
|
||||
(date, str) -> <s> formatted date
|
||||
(date, str, utc) -> <s> formatted date
|
||||
str <s> format string
|
||||
(string) -> <s> formatted date
|
||||
(date, string) -> <s> formatted date
|
||||
(date, string, utc) -> <s> formatted date
|
||||
string <s> format string
|
||||
date <d|n|s> date
|
||||
utc <b> date is utc
|
||||
<script>
|
||||
|
@ -129,77 +128,161 @@ Ox.formatDate <f> Formats a date according to a format string
|
|||
'%'
|
||||
@*/
|
||||
|
||||
Ox.formatDate = function(date, str, utc) {
|
||||
if (date === '') {
|
||||
return '';
|
||||
}
|
||||
date = Ox.makeDate(date);
|
||||
(function() {
|
||||
|
||||
var format = [
|
||||
['%', function() {return '%{%}';}],
|
||||
['c', function() {return '%D %r';}],
|
||||
['D', function() {return '%m/%d/%y';}],
|
||||
['F', function() {return '%Y-%m-%d';}],
|
||||
['h', function() {return '%b';}],
|
||||
['R', function() {return '%H:%M';}],
|
||||
['r', function() {return '%I:%M:%S %p';}],
|
||||
['T', function() {return '%H:%M:%S';}],
|
||||
['v', function() {return '%e-%b-%Y';}],
|
||||
['\\+', function() {return '%a %b %e %H:%M:%S %Z %Y';}],
|
||||
['A', function(d) {return Ox.WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
|
||||
['a', function(d) {return Ox.SHORT_WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
|
||||
['B', function(d) {return Ox.MONTHS[Ox.getMonth(d, utc)];}],
|
||||
['b', function(d) {return Ox.SHORT_MONTHS[Ox.getMonth(d, utc)];}],
|
||||
['C', function(d) {return Math.floor(Ox.getFullYear(d, utc) / 100).toString();}],
|
||||
['d', function(d) {return Ox.pad(Ox.getDate(d, utc), 2);}],
|
||||
['e', function(d) {return Ox.pad(Ox.getDate(d, utc), 2, ' ');}],
|
||||
['G', function(d) {return Ox.getISOYear(d, utc);}],
|
||||
['g', function(d) {return Ox.getISOYear(d, utc).toString().slice(-2);}],
|
||||
['H', function(d) {return Ox.pad(Ox.getHours(d, utc), 2);}],
|
||||
['I', function(d) {return Ox.pad((Ox.getHours(d, utc) + 11) % 12 + 1, 2);}],
|
||||
['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d, utc), 3);}],
|
||||
['k', function(d) {return Ox.pad(Ox.getHours(d, utc), 2, ' ');}],
|
||||
['l', function(d) {return Ox.pad(((Ox.getHours(d, utc) + 11) % 12 + 1), 2, ' ');}],
|
||||
['M', function(d) {return Ox.pad(Ox.getMinutes(d, utc), 2);}],
|
||||
['m', function(d) {return Ox.pad((Ox.getMonth(d, utc) + 1), 2);}],
|
||||
['p', function(d) {return Ox.AMPM[Math.floor(Ox.getHours(d, utc) / 12)];}],
|
||||
['Q', function(d) {return Math.floor(Ox.getMonth(d, utc) / 4) + 1;}],
|
||||
['S', function(d) {return Ox.pad(Ox.getSeconds(d, utc), 2);}],
|
||||
['s', function(d) {
|
||||
return Math.floor(d.getTime() / 1000)
|
||||
['%', function() {
|
||||
return '%{%}';
|
||||
}],
|
||||
['c', function() {
|
||||
return '%D %r';
|
||||
}],
|
||||
['D', function() {
|
||||
return '%m/%d/%y';
|
||||
}],
|
||||
['F', function() {
|
||||
return '%Y-%m-%d';
|
||||
}],
|
||||
['h', function() {
|
||||
return '%b';
|
||||
}],
|
||||
['R', function() {
|
||||
return '%H:%M';
|
||||
}],
|
||||
['r', function() {
|
||||
return '%I:%M:%S %p';
|
||||
}],
|
||||
['T', function() {
|
||||
return '%H:%M:%S';
|
||||
}],
|
||||
['v', function() {
|
||||
return '%e-%b-%Y';
|
||||
}],
|
||||
['\\+', function() {
|
||||
return '%a %b %e %H:%M:%S %Z %Y';
|
||||
}],
|
||||
['A', function(date, utc) {
|
||||
return Ox.WEEKDAYS[(Ox.getDay(date, utc) + 6) % 7];
|
||||
}],
|
||||
['a', function(date, utc) {
|
||||
return Ox.SHORT_WEEKDAYS[(Ox.getDay(date, utc) + 6) % 7];
|
||||
}],
|
||||
['B', function(date, utc) {
|
||||
return Ox.MONTHS[Ox.getMonth(date, utc)];
|
||||
}],
|
||||
['b', function(date, utc) {
|
||||
return Ox.SHORT_MONTHS[Ox.getMonth(date, utc)];
|
||||
}],
|
||||
['C', function(date, utc) {
|
||||
return Math.floor(Ox.getFullYear(date, utc) / 100).toString();
|
||||
}],
|
||||
['d', function(date, utc) {
|
||||
return Ox.pad(Ox.getDate(date, utc), 2);
|
||||
}],
|
||||
['e', function(date, utc) {
|
||||
return Ox.pad(Ox.getDate(date, utc), 2, ' ');
|
||||
}],
|
||||
['G', function(date, utc) {
|
||||
return Ox.getISOYear(date, utc);
|
||||
}],
|
||||
['g', function(date, utc) {
|
||||
return Ox.getISOYear(date, utc).toString().slice(-2);
|
||||
}],
|
||||
['H', function(date, utc) {
|
||||
return Ox.pad(Ox.getHours(date, utc), 2);
|
||||
}],
|
||||
['I', function(date, utc) {
|
||||
return Ox.pad((Ox.getHours(date, utc) + 11) % 12 + 1, 2);
|
||||
}],
|
||||
['j', function(date, utc) {
|
||||
return Ox.pad(Ox.getDayOfTheYear(date, utc), 3);
|
||||
}],
|
||||
['k', function(date, utc) {
|
||||
return Ox.pad(Ox.getHours(date, utc), 2, ' ');
|
||||
}],
|
||||
['l', function(date, utc) {
|
||||
return Ox.pad(((Ox.getHours(date, utc) + 11) % 12 + 1), 2, ' ');
|
||||
}],
|
||||
['M', function(date, utc) {
|
||||
return Ox.pad(Ox.getMinutes(date, utc), 2);
|
||||
}],
|
||||
['m', function(date, utc) {
|
||||
return Ox.pad((Ox.getMonth(date, utc) + 1), 2);
|
||||
}],
|
||||
['p', function(date, utc) {
|
||||
return Ox.AMPM[Math.floor(Ox.getHours(date, utc) / 12)];
|
||||
}],
|
||||
['Q', function(date, utc) {
|
||||
return Math.floor(Ox.getMonth(date, utc) / 4) + 1;
|
||||
}],
|
||||
['S', function(date, utc) {
|
||||
return Ox.pad(Ox.getSeconds(date, utc), 2);
|
||||
}],
|
||||
['s', function(date, utc) {
|
||||
return Math.floor(date.getTime() / 1000)
|
||||
- (utc ? Ox.getTimezoneOffset() / 1000 : 0);
|
||||
}],
|
||||
['U', function(d) {return Ox.pad(Ox.getWeek(d, utc), 2);}],
|
||||
['u', function(d) {return Ox.getISODay(d, utc);}],
|
||||
['V', function(d) {return Ox.pad(Ox.getISOWeek(d, utc), 2);}],
|
||||
['W', function(d) {
|
||||
return Ox.pad(Math.floor((Ox.getDayOfTheYear(d, utc)
|
||||
+ (Ox.getFirstDayOfTheYear(d, utc) || 7) - 2) / 7), 2);
|
||||
['U', function(date, utc) {
|
||||
return Ox.pad(Ox.getWeek(date, utc), 2);
|
||||
}],
|
||||
['w', function(d) {return Ox.getDay(d, utc);}],
|
||||
['X', function(d) {
|
||||
var y = Ox.getFullYear(d, utc);
|
||||
['u', function(date, utc) {
|
||||
return Ox.getISODay(date, utc);
|
||||
}],
|
||||
['V', function(date, utc) {
|
||||
return Ox.pad(Ox.getISOWeek(date, utc), 2);
|
||||
}],
|
||||
['W', function(date, utc) {
|
||||
return Ox.pad(Math.floor((Ox.getDayOfTheYear(date, utc)
|
||||
+ (Ox.getFirstDayOfTheYear(date, utc) || 7) - 2) / 7), 2);
|
||||
}],
|
||||
['w', function(date, utc) {
|
||||
return Ox.getDay(date, utc);
|
||||
}],
|
||||
['X', function(date, utc) {
|
||||
var y = Ox.getFullYear(date, utc);
|
||||
return Math.abs(y) + ' ' + Ox.BCAD[y < 0 ? 0 : 1];
|
||||
}],
|
||||
['x', function(d) {
|
||||
var y = Ox.getFullYear(d, utc);
|
||||
['x', function(date, utc) {
|
||||
var y = Ox.getFullYear(date, utc);
|
||||
return Math.abs(y) + (y < 1000 ? ' ' + Ox.BCAD[y < 0 ? 0 : 1] : '');
|
||||
}],
|
||||
['Y', function(d) {return Ox.getFullYear(d, utc);}],
|
||||
['y', function(d) {return Ox.getFullYear(d, utc).toString().slice(-2);}],
|
||||
['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}],
|
||||
['z', function(d) {return Ox.getTimezoneOffsetString(d);}],
|
||||
['n', function() {return '\n';}],
|
||||
['t', function() {return '\t';}],
|
||||
['\\{%\\}', function() {return '%';}]
|
||||
];
|
||||
format.forEach(function(v) {
|
||||
var regexp = new RegExp('%' + v[0], 'g');
|
||||
if (regexp.test(str)) {
|
||||
str = str.replace(regexp, v[1](date));
|
||||
}
|
||||
['Y', function(date, utc) {
|
||||
return Ox.getFullYear(date, utc);
|
||||
}],
|
||||
['y', function(date, utc) {
|
||||
return Ox.getFullYear(date, utc).toString().slice(-2);
|
||||
}],
|
||||
['Z', function(date) {
|
||||
return date.toString().split('(')[1].replace(')', '');
|
||||
}],
|
||||
['z', function(date) {
|
||||
return Ox.getTimezoneOffsetString(date);
|
||||
}],
|
||||
['n', function() {
|
||||
return '\n';
|
||||
}],
|
||||
['t', function() {
|
||||
return '\t';
|
||||
}],
|
||||
['\\{%\\}', function() {
|
||||
return '%';
|
||||
}]
|
||||
].map(function(value) {
|
||||
return [new RegExp('%' + value[0], 'g'), value[1]];
|
||||
});
|
||||
return str;
|
||||
};
|
||||
|
||||
Ox.formatDate = function(date, string, utc) {
|
||||
if (date === '') {
|
||||
return '';
|
||||
}
|
||||
date = Ox.makeDate(date);
|
||||
format.forEach(function(value) {
|
||||
string = string.replace(value[0], value[1](date, utc));
|
||||
});
|
||||
return string;
|
||||
};
|
||||
|
||||
})();
|
||||
|
||||
/*@
|
||||
Ox.formatDateRange <f> Formats a date range as a string
|
||||
|
|
Loading…
Reference in a new issue