Permalink
Browse files

remove duplicate calls, simplify date format methods

  • Loading branch information...
1 parent b8efc15 commit e8435fe8539e5b810e8b6af1ab33af488b41b58f @paularmstrong committed Dec 27, 2011
Showing with 12 additions and 6 deletions.
  1. +12 −6 lib/dateformat.js
View
@@ -25,7 +25,8 @@ exports.N = function (input) {
return input.getDay();
};
exports.S = function (input) {
- return (input.getDate() % 10 === 1 && input.getDate() !== 11 ? 'st' : (input.getDate() % 10 === 2 && input.getDate() !== 12 ? 'nd' : (input.getDate() % 10 === 3 && input.getDate() !== 13 ? 'rd' : 'th')));
+ var d = input.getDate();
+ return (d % 10 === 1 && d !== 11 ? 'st' : (d % 10 === 2 && d !== 12 ? 'nd' : (d % 10 === 3 && d !== 13 ? 'rd' : 'th')));
};
exports.w = function (input) {
return input.getDay() - 1;
@@ -98,7 +99,8 @@ exports.B = function (input) {
return ('000'.concat(beats).slice(beats.length));
};
exports.g = function (input) {
- return input.getHours() === 0 ? 12 : (input.getHours() > 12 ? input.getHours() - 12 : input.getHours());
+ var h = input.getHours();
+ return h === 0 ? 12 : (h > 12 ? h - 12 : h);
};
exports.G = function (input) {
return input.getHours();
@@ -108,21 +110,25 @@ exports.h = function (input) {
return ((h < 10 || (12 < h && 22 > h)) ? '0' : '') + ((h < 12) ? h : h - 12);
};
exports.H = function (input) {
- return (input.getHours() < 10 ? '0' : '') + input.getHours();
+ var h = input.getHours();
+ return (h < 10 ? '0' : '') + h;
};
exports.i = function (input) {
- return (input.getMinutes() < 10 ? '0' : '') + input.getMinutes();
+ var m = input.getMinutes();
+ return (m < 10 ? '0' : '') + m;
};
exports.s = function (input) {
- return (input.getSeconds() < 10 ? '0' : '') + input.getSeconds();
+ var s = input.getSeconds();
+ return (s < 10 ? '0' : '') + s;
};
//u = function () { return ''; },
// Timezone
//e = function () { return ''; },
//I = function () { return ''; },
exports.O = function (input) {
- return (input.getTimezoneOffset() < 0 ? '-' : '+') + (input.getTimezoneOffset() / 60 < 10 ? '0' : '') + (input.getTimezoneOffset() / 60) + '00';
+ var tz = input.getTimezoneOffset();
+ return (tz < 0 ? '-' : '+') + (tz / 60 < 10 ? '0' : '') + (tz / 60) + '00';
};
//T = function () { return ''; },
exports.Z = function (input) {

0 comments on commit e8435fe

Please sign in to comment.