date-and-time
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -17,28 +17,28 @@ /** | ||
A: ['a.m.', 'p.m.'], | ||
formats: { | ||
YYYY: function (d) { return ('000' + d.getFullYear()).slice(-4); }, | ||
YY: function (d) { return ('0' + d.getFullYear()).slice(-2); }, | ||
Y: function (d) { return '' + d.getFullYear(); }, | ||
MMMM: function (d) { return this.MMMM[d.getMonth()]; }, | ||
MMM: function (d) { return this.MMM[d.getMonth()]; }, | ||
MM: function (d) { return ('0' + (d.getMonth() + 1)).slice(-2); }, | ||
M: function (d) { return '' + (d.getMonth() + 1); }, | ||
DD: function (d) { return ('0' + d.getDate()).slice(-2); }, | ||
D: function (d) { return '' + d.getDate(); }, | ||
HH: function (d) { return ('0' + d.getHours()).slice(-2); }, | ||
H: function (d) { return '' + d.getHours(); }, | ||
A: function (d) { return this.A[d.getHours() > 11 | 0]; }, | ||
hh: function (d) { return ('0' + (d.getHours() % 12 || 12)).slice(-2); }, | ||
h: function (d) { return '' + (d.getHours() % 12 || 12); }, | ||
mm: function (d) { return ('0' + d.getMinutes()).slice(-2); }, | ||
m: function (d) { return '' + d.getMinutes(); }, | ||
ss: function (d) { return ('0' + d.getSeconds()).slice(-2); }, | ||
s: function (d) { return '' + d.getSeconds(); }, | ||
SSS: function (d) { return ('00' + d.getMilliseconds()).slice(-3); }, | ||
SS: function (d) { return ('0' + (d.getMilliseconds() / 10 | 0)).slice(-2); }, | ||
S: function (d) { return '' + (d.getMilliseconds() / 100 | 0); }, | ||
dddd: function (d) { return this.dddd[d.getDay()]; }, | ||
ddd: function (d) { return this.ddd[d.getDay()]; }, | ||
dd: function (d) { return this.dd[d.getDay()]; }, | ||
Z: function (d) { | ||
formatter: { | ||
YYYY: function (d/*, formatString*/) { return ('000' + d.getFullYear()).slice(-4); }, | ||
YY: function (d/*, formatString*/) { return ('0' + d.getFullYear()).slice(-2); }, | ||
Y: function (d/*, formatString*/) { return '' + d.getFullYear(); }, | ||
MMMM: function (d/*, formatString*/) { return this.MMMM[d.getMonth()]; }, | ||
MMM: function (d/*, formatString*/) { return this.MMM[d.getMonth()]; }, | ||
MM: function (d/*, formatString*/) { return ('0' + (d.getMonth() + 1)).slice(-2); }, | ||
M: function (d/*, formatString*/) { return '' + (d.getMonth() + 1); }, | ||
DD: function (d/*, formatString*/) { return ('0' + d.getDate()).slice(-2); }, | ||
D: function (d/*, formatString*/) { return '' + d.getDate(); }, | ||
HH: function (d/*, formatString*/) { return ('0' + d.getHours()).slice(-2); }, | ||
H: function (d/*, formatString*/) { return '' + d.getHours(); }, | ||
A: function (d/*, formatString*/) { return this.A[d.getHours() > 11 | 0]; }, | ||
hh: function (d/*, formatString*/) { return ('0' + (d.getHours() % 12 || 12)).slice(-2); }, | ||
h: function (d/*, formatString*/) { return '' + (d.getHours() % 12 || 12); }, | ||
mm: function (d/*, formatString*/) { return ('0' + d.getMinutes()).slice(-2); }, | ||
m: function (d/*, formatString*/) { return '' + d.getMinutes(); }, | ||
ss: function (d/*, formatString*/) { return ('0' + d.getSeconds()).slice(-2); }, | ||
s: function (d/*, formatString*/) { return '' + d.getSeconds(); }, | ||
SSS: function (d/*, formatString*/) { return ('00' + d.getMilliseconds()).slice(-3); }, | ||
SS: function (d/*, formatString*/) { return ('0' + (d.getMilliseconds() / 10 | 0)).slice(-2); }, | ||
S: function (d/*, formatString*/) { return '' + (d.getMilliseconds() / 100 | 0); }, | ||
dddd: function (d/*, formatString*/) { return this.dddd[d.getDay()]; }, | ||
ddd: function (d/*, formatString*/) { return this.ddd[d.getDay()]; }, | ||
dd: function (d/*, formatString*/) { return this.dd[d.getDay()]; }, | ||
Z: function (d/*, formatString*/) { | ||
var offset = d.utc ? 0 : d.getTimezoneOffset() / 0.6; | ||
@@ -49,3 +49,24 @@ return (offset > 0 ? '-' : '+') + ('000' + Math.abs(offset - offset % 100 * 0.4)).slice(-4); | ||
}, | ||
parsers: { | ||
parser: { | ||
find: function (array, str) { | ||
var index = -1, length = 0; | ||
for (var i = 0, len = array.length, item; i < len; i++) { | ||
item = array[i]; | ||
if (!str.indexOf(item) && item.length > length) { | ||
index = i; | ||
length = item.length; | ||
} | ||
} | ||
return { index: index, length: length }; | ||
}, | ||
MMMM: function (str/*, formatString*/) { | ||
return this.parser.find(this.MMMM, str); | ||
}, | ||
MMM: function (str/*, formatString*/) { | ||
return this.parser.find(this.MMM, str); | ||
}, | ||
A: function (str/*, formatString*/) { | ||
return this.parser.find(this.A, str); | ||
}, | ||
h: function (h, a) { return (h === 12 ? 0 : h) + a * 12; }, | ||
@@ -58,44 +79,2 @@ pre: function (str) { return str; } | ||
return typeof module === 'object' && typeof module.exports === 'object'; | ||
}, | ||
forEach = function (array, fn) { | ||
for (var i = 0, len = array.length; i < len; i++) { | ||
if (fn(array[i], i) === 0) { | ||
break; | ||
} | ||
} | ||
}, | ||
parse = function (dateString, formatString) { | ||
var offset = 0, k, length, str, | ||
keys = formatString.match(/YYYY|YY|MMM?M?|DD|HH|hh|mm|ss|SSS?|./g), | ||
dt = { Y: 0, M: 1, D: 1, A: 0, H: 0, h: 0, m: 0, s: 0, S: 0 }; | ||
dateString = locales[lang].parsers.pre(dateString); | ||
forEach(keys, function (key) { | ||
k = key.charAt(0); | ||
length = key.length; | ||
str = dateString.slice(offset); | ||
if (/^(MM|DD|HH|hh|mm|ss|SS?S?)$/.test(key)) { | ||
dt[k] = str.slice(0, length) | 0; | ||
} else if (/^(YYYY|YY|M|D|H|h|m|s)$/.test(key)) { | ||
str = (str.match(length === 4 ? /^\d{1,4}/ : /^\d\d?/) || [''])[0]; | ||
length = str.length; | ||
dt[k] = str | 0; | ||
if (k === 'Y' && dt.Y < 70) { | ||
dt.Y += 2000; | ||
} | ||
} else if (/^(MMMM?|A)$/.test(key)) { | ||
forEach(locales[lang][key], function (val, i) { | ||
if (!str.indexOf(val)) { | ||
dt[k] = k === 'M' ? i + 1 : i; | ||
length = val.length; | ||
return 0; | ||
} | ||
}); | ||
} | ||
offset += length; | ||
}); | ||
return dt; | ||
}, | ||
toH = function (h, a) { | ||
return locales[lang].parsers.h(h, a); | ||
}; | ||
@@ -105,3 +84,3 @@ | ||
* formatting a date | ||
* @param {Object} dateObj - target date | ||
* @param {Object} dateObj - date object | ||
* @param {String} formatString - format string | ||
@@ -112,9 +91,9 @@ * @param {Boolean} [utc] - output as UTC | ||
date.format = function (dateObj, formatString, utc) { | ||
var d = new Date(dateObj.getTime() + (utc ? dateObj.getTimezoneOffset() * 60000 : 0)), | ||
locale = locales[lang], formats = locale.formats; | ||
var d = date.addMinutes(dateObj, utc ? dateObj.getTimezoneOffset() : 0), | ||
locale = locales[lang], formatter = locale.formatter; | ||
d.utc = utc; | ||
return formatString.replace(/(\[[^\[\]]*]|\[.*\][^\[]*\]|YYYY|YY|MMM?M?|DD|HH|hh|mm|ss|SSS?|ddd?d?|.)/g, function (token) { | ||
var format = formats[token]; | ||
return format ? formats.post(format.call(locale, d)) : token.replace(/\[(.*)]/, '$1'); | ||
var format = formatter[token]; | ||
return format ? formatter.post(format.call(locale, d, formatString)) : token.replace(/\[(.*)]/, '$1'); | ||
}); | ||
@@ -131,6 +110,44 @@ }; | ||
date.parse = function (dateString, formatString, utc) { | ||
var dt = parse(dateString, formatString), | ||
dateObj = new Date(dt.Y, dt.M - 1, dt.D, dt.H || toH(dt.h, dt.A), dt.m, dt.s, dt.S); | ||
var locale = locales[lang], dString = locale.parser.pre(dateString), | ||
offset = 0, keys, i, token, length, p, str, result, dateObj, | ||
re = /(MMMM?|A)|(YYYY)|(SSS)|(MM|DD|HH|hh|mm|ss)|(YY|M|D|H|h|m|s|SS)|(S)|(.)/g, | ||
exp = { 2: /^\d{1,4}/, 3: /^\d{1,3}/, 4: /^\d\d/, 5: /^\d\d?/, 6: /^\d/ }, | ||
dt = { Y: 1970, M: 1, D: 1, H: 0, m: 0, s: 0, S: 0 }; | ||
return utc ? new Date(dateObj.getTime() - dateObj.getTimezoneOffset() * 60000) : dateObj; | ||
while ((keys = re.exec(formatString))) { | ||
for (i = 0, length = 1, token = ''; !token;) { | ||
token = keys[++i]; | ||
} | ||
p = token.charAt(0); | ||
str = dString.slice(offset); | ||
if (i < 2) { | ||
result = locale.parser[token].call(locale, str, formatString); | ||
dt[p] = result.index; | ||
if (p === 'M') { | ||
dt[p]++; | ||
} | ||
length = result.length; | ||
if (!length) { | ||
return NaN; | ||
} | ||
} else if (i < 7) { | ||
str = (str.match(exp[i]) || [''])[0]; | ||
if (!str) { | ||
return NaN; | ||
} | ||
dt[p] = (p === 'S' ? (str + '000').slice(0, -token.length) : str) | 0; | ||
length = str.length; | ||
} | ||
offset += length; | ||
} | ||
dt.Y += dt.Y < 70 ? 2000 : dt.Y < 100 ? 1900 : 0; | ||
dt.H = dt.H || locale.parser.h(dt.h || 0, dt.A || 0); | ||
dateObj = new Date(dt.Y, dt.M - 1, dt.D, dt.H, dt.m, dt.s, dt.S); | ||
if (dt.Y !== dateObj.getFullYear() || dt.M - 1 !== dateObj.getMonth() || dt.D !== dateObj.getDate() | ||
|| dt.H !== dateObj.getHours() || dt.m !== dateObj.getMinutes() || dt.s !== dateObj.getSeconds() | ||
|| dt.S !== dateObj.getMilliseconds()) { | ||
return NaN; | ||
} | ||
return utc ? date.addMinutes(dateObj, -dateObj.getTimezoneOffset()) : dateObj; | ||
}; | ||
@@ -145,9 +162,3 @@ | ||
date.isValid = function (dateString, formatString) { | ||
var dt = parse(dateString, formatString), | ||
H = dt.H || toH(dt.h, dt.A), | ||
d = new Date(dt.Y, dt.M - 1, dt.D, H, dt.m, dt.s, dt.S); | ||
return dt.Y === d.getFullYear() && dt.M - 1 === d.getMonth() && dt.D === d.getDate() | ||
&& H === d.getHours() && dt.m === d.getMinutes() && dt.s === d.getSeconds() | ||
&& dt.S === d.getMilliseconds(); | ||
return !!date.parse(dateString, formatString); | ||
}; | ||
@@ -157,11 +168,8 @@ | ||
* adding years | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} years - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} years - adding year | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
date.addYears = function (dateObj, years) { | ||
var d = new Date(dateObj.getTime()); | ||
d.setFullYear(d.getFullYear() + years); | ||
return d; | ||
return date.addMonths(dateObj, years * 12); | ||
}; | ||
@@ -171,5 +179,5 @@ | ||
* adding months | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} months - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} months - adding month | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
@@ -185,8 +193,8 @@ date.addMonths = function (dateObj, months) { | ||
* adding days | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} days - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} days - adding day | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
date.addDays = function (dateObj, days) { | ||
return new Date(dateObj.getTime() + days * 86400000); | ||
return date.addMilliseconds(dateObj, days * 86400000); | ||
}; | ||
@@ -196,8 +204,8 @@ | ||
* adding hours | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} hours - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} hours - adding hour | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
date.addHours = function (dateObj, hours) { | ||
return new Date(dateObj.getTime() + hours * 3600000); | ||
return date.addMilliseconds(dateObj, hours * 3600000); | ||
}; | ||
@@ -207,8 +215,8 @@ | ||
* adding minutes | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} minutes - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} minutes - adding minute | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
date.addMinutes = function (dateObj, minutes) { | ||
return new Date(dateObj.getTime() + minutes * 60000); | ||
return date.addMilliseconds(dateObj, minutes * 60000); | ||
}; | ||
@@ -218,8 +226,8 @@ | ||
* adding seconds | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} seconds - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} seconds - adding second | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
date.addSeconds = function (dateObj, seconds) { | ||
return new Date(dateObj.getTime() + seconds * 1000); | ||
return date.addMilliseconds(dateObj, seconds * 1000); | ||
}; | ||
@@ -229,5 +237,5 @@ | ||
* adding milliseconds | ||
* @param {Object} dateObj - the augend | ||
* @param {Number} milliseconds - the addend | ||
* @returns {Object} the date after addition | ||
* @param {Object} dateObj - date object | ||
* @param {Number} milliseconds - adding millisecond | ||
* @returns {Object} the date after adding the value | ||
*/ | ||
@@ -240,5 +248,5 @@ date.addMilliseconds = function (dateObj, milliseconds) { | ||
* subtracting | ||
* @param {Object} date1 - the minuend | ||
* @param {Object} date2 - the subtrahend | ||
* @returns {Object} the result object after subtraction | ||
* @param {Object} date1 - date object | ||
* @param {Object} date2 - date object | ||
* @returns {Object} the result object after subtracting the date | ||
*/ | ||
@@ -269,3 +277,3 @@ date.subtract = function (date1, date2) { | ||
* leap year | ||
* @param {Object} dateObj - target date | ||
* @param {Object} dateObj - date object | ||
* @returns {Boolean} whether the year is a leap year | ||
@@ -279,2 +287,12 @@ */ | ||
/** | ||
* comparison of dates | ||
* @param {Object} date1 - target for comparison | ||
* @param {Object} date2 - target for comparison | ||
* @returns {Boolean} whether the dates are the same day (times are ignored) | ||
*/ | ||
date.isSameDay = function (date1, date2) { | ||
return date.format(date1, 'YYYYMMDD') === date.format(date2, 'YYYYMMDD'); | ||
}; | ||
/** | ||
* setting a locale | ||
@@ -325,7 +343,7 @@ * @param {String} [code] - language code | ||
if (options.formats) { | ||
locale.formats = copy(options.formats, base.formats); | ||
if (options.formatter) { | ||
locale.formatter = copy(options.formatter, base.formatter); | ||
} | ||
if (options.parsers) { | ||
locale.parsers = copy(options.parsers, base.parsers); | ||
if (options.parser) { | ||
locale.parser = copy(options.parser, base.parser); | ||
} | ||
@@ -346,2 +364,1 @@ locales[code] = locale; | ||
}(this)); | ||
/* | ||
date-and-time.js (c) KNOWLEDGECODE | MIT | ||
*/ | ||
(function(p){var d={},h="en",g={en:{MMMM:"January February March April May June July August September October November December".split(" "),MMM:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),dddd:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),ddd:"Sun Mon Tue Wed Thu Fri Sat".split(" "),dd:"Su Mo Tu We Th Fr Sa".split(" "),A:["a.m.","p.m."],formats:{YYYY:function(a){return("000"+a.getFullYear()).slice(-4)},YY:function(a){return("0"+a.getFullYear()).slice(-2)}, | ||
(function(t){var c={},n="en",l={en:{MMMM:"January February March April May June July August September October November December".split(" "),MMM:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),dddd:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),ddd:"Sun Mon Tue Wed Thu Fri Sat".split(" "),dd:"Su Mo Tu We Th Fr Sa".split(" "),A:["a.m.","p.m."],formatter:{YYYY:function(a){return("000"+a.getFullYear()).slice(-4)},YY:function(a){return("0"+a.getFullYear()).slice(-2)}, | ||
Y:function(a){return""+a.getFullYear()},MMMM:function(a){return this.MMMM[a.getMonth()]},MMM:function(a){return this.MMM[a.getMonth()]},MM:function(a){return("0"+(a.getMonth()+1)).slice(-2)},M:function(a){return""+(a.getMonth()+1)},DD:function(a){return("0"+a.getDate()).slice(-2)},D:function(a){return""+a.getDate()},HH:function(a){return("0"+a.getHours()).slice(-2)},H:function(a){return""+a.getHours()},A:function(a){return this.A[11<a.getHours()|0]},hh:function(a){return("0"+(a.getHours()%12||12)).slice(-2)}, | ||
h:function(a){return""+(a.getHours()%12||12)},mm:function(a){return("0"+a.getMinutes()).slice(-2)},m:function(a){return""+a.getMinutes()},ss:function(a){return("0"+a.getSeconds()).slice(-2)},s:function(a){return""+a.getSeconds()},SSS:function(a){return("00"+a.getMilliseconds()).slice(-3)},SS:function(a){return("0"+(a.getMilliseconds()/10|0)).slice(-2)},S:function(a){return""+(a.getMilliseconds()/100|0)},dddd:function(a){return this.dddd[a.getDay()]},ddd:function(a){return this.ddd[a.getDay()]},dd:function(a){return this.dd[a.getDay()]}, | ||
Z:function(a){a=a.utc?0:a.getTimezoneOffset()/.6;return(0<a?"-":"+")+("000"+Math.abs(a-a%100*.4)).slice(-4)},post:function(a){return a}},parsers:{h:function(a,c){return(12===a?0:a)+12*c},pre:function(a){return a}}}},l=function(){return"object"===typeof module&&"object"===typeof module.exports},m=function(a,c){for(var b=0,d=a.length;b<d&&0!==c(a[b],b);b++);},n=function(a,c){var b=0,d,e,f,q=c.match(/YYYY|YY|MMM?M?|DD|HH|hh|mm|ss|SSS?|./g),k={Y:0,M:1,D:1,A:0,H:0,h:0,m:0,s:0,S:0};a=g[h].parsers.pre(a); | ||
m(q,function(c){d=c.charAt(0);e=c.length;f=a.slice(b);/^(MM|DD|HH|hh|mm|ss|SS?S?)$/.test(c)?k[d]=f.slice(0,e)|0:/^(YYYY|YY|M|D|H|h|m|s)$/.test(c)?(f=(f.match(4===e?/^\d{1,4}/:/^\d\d?/)||[""])[0],e=f.length,k[d]=f|0,"Y"===d&&70>k.Y&&(k.Y+=2E3)):/^(MMMM?|A)$/.test(c)&&m(g[h][c],function(a,b){if(!f.indexOf(a))return k[d]="M"===d?b+1:b,e=a.length,0});b+=e});return k};d.format=function(a,c,b){var d=new Date(a.getTime()+(b?6E4*a.getTimezoneOffset():0)),e=g[h],f=e.formats;d.utc=b;return c.replace(/(\[[^\[\]]*]|\[.*\][^\[]*\]|YYYY|YY|MMM?M?|DD|HH|hh|mm|ss|SSS?|ddd?d?|.)/g, | ||
function(a){var b=f[a];return b?f.post(b.call(e,d)):a.replace(/\[(.*)]/,"$1")})};d.parse=function(a,c,b){a=n(a,c);a=new Date(a.Y,a.M-1,a.D,a.H||g[h].parsers.h(a.h,a.A),a.m,a.s,a.S);return b?new Date(a.getTime()-6E4*a.getTimezoneOffset()):a};d.isValid=function(a,c){var b=n(a,c),d=b.H||g[h].parsers.h(b.h,b.A),e=new Date(b.Y,b.M-1,b.D,d,b.m,b.s,b.S);return b.Y===e.getFullYear()&&b.M-1===e.getMonth()&&b.D===e.getDate()&&d===e.getHours()&&b.m===e.getMinutes()&&b.s===e.getSeconds()&&b.S===e.getMilliseconds()}; | ||
d.addYears=function(a,c){var b=new Date(a.getTime());b.setFullYear(b.getFullYear()+c);return b};d.addMonths=function(a,c){var b=new Date(a.getTime());b.setMonth(b.getMonth()+c);return b};d.addDays=function(a,c){return new Date(a.getTime()+864E5*c)};d.addHours=function(a,c){return new Date(a.getTime()+36E5*c)};d.addMinutes=function(a,c){return new Date(a.getTime()+6E4*c)};d.addSeconds=function(a,c){return new Date(a.getTime()+1E3*c)};d.addMilliseconds=function(a,c){return new Date(a.getTime()+c)}; | ||
d.subtract=function(a,c){var b=a.getTime()-c.getTime();return{toMilliseconds:function(){return b},toSeconds:function(){return b/1E3|0},toMinutes:function(){return b/6E4|0},toHours:function(){return b/36E5|0},toDays:function(){return b/864E5|0}}};d.isLeapYear=function(a){a=a.getFullYear();return!(a%4)&&!!(a%100)||!(a%400)};d.locale=function(a){a&&("en"!==a&&l()&&require("./locale/"+a),h=a);return h};d.getLocales=function(a){return g[a||h]};d.setLocales=function(a,c){var b=function(a,b){var c=function(){}, | ||
d;c.prototype=b;c=new c;for(d in a)a.hasOwnProperty(d)&&(c[d]=a[d]);return c},d=g[a]||g.en,e=b(c,d);c.formats&&(e.formats=b(c.formats,d.formats));c.parsers&&(e.parsers=b(c.parsers,d.parsers));g[a]=e};l()?module.exports=d:"function"===typeof define&&define.amd?define([],function(){return d}):p.date=d})(this); | ||
Z:function(a){a=a.utc?0:a.getTimezoneOffset()/.6;return(0<a?"-":"+")+("000"+Math.abs(a-a%100*.4)).slice(-4)},post:function(a){return a}},parser:{find:function(a,b){for(var c=-1,d=0,e=0,k=a.length,f;e<k;e++)f=a[e],!b.indexOf(f)&&f.length>d&&(c=e,d=f.length);return{index:c,length:d}},MMMM:function(a){return this.parser.find(this.MMMM,a)},MMM:function(a){return this.parser.find(this.MMM,a)},A:function(a){return this.parser.find(this.A,a)},h:function(a,b){return(12===a?0:a)+12*b},pre:function(a){return a}}}}, | ||
r=function(){return"object"===typeof module&&"object"===typeof module.exports};c.format=function(a,b,q){var d=c.addMinutes(a,q?a.getTimezoneOffset():0),e=l[n],k=e.formatter;d.utc=q;return b.replace(/(\[[^\[\]]*]|\[.*\][^\[]*\]|YYYY|YY|MMM?M?|DD|HH|hh|mm|ss|SSS?|ddd?d?|.)/g,function(a){var c=k[a];return c?k.post(c.call(e,d,b)):a.replace(/\[(.*)]/,"$1")})};c.parse=function(a,b,q){var d=l[n],e=d.parser.pre(a),k=0,f,g,m,p,h,r=/(MMMM?|A)|(YYYY)|(SSS)|(MM|DD|HH|hh|mm|ss)|(YY|M|D|H|h|m|s|SS)|(S)|(.)/g,t= | ||
{2:/^\d{1,4}/,3:/^\d{1,3}/,4:/^\d\d/,5:/^\d\d?/,6:/^\d/};for(a={Y:1970,M:1,D:1,H:0,m:0,s:0,S:0};f=r.exec(b);){g=0;p=1;for(m="";!m;)m=f[++g];f=m.charAt(0);h=e.slice(k);if(2>g){if(g=d.parser[m].call(d,h,b),a[f]=g.index,"M"===f&&a[f]++,p=g.length,!p)return NaN}else if(7>g){h=(h.match(t[g])||[""])[0];if(!h)return NaN;a[f]=("S"===f?(h+"000").slice(0,-m.length):h)|0;p=h.length}k+=p}a.Y+=70>a.Y?2E3:100>a.Y?1900:0;a.H=a.H||d.parser.h(a.h||0,a.A||0);b=new Date(a.Y,a.M-1,a.D,a.H,a.m,a.s,a.S);return a.Y!==b.getFullYear()|| | ||
a.M-1!==b.getMonth()||a.D!==b.getDate()||a.H!==b.getHours()||a.m!==b.getMinutes()||a.s!==b.getSeconds()||a.S!==b.getMilliseconds()?NaN:q?c.addMinutes(b,-b.getTimezoneOffset()):b};c.isValid=function(a,b){return!!c.parse(a,b)};c.addYears=function(a,b){return c.addMonths(a,12*b)};c.addMonths=function(a,b){var c=new Date(a.getTime());c.setMonth(c.getMonth()+b);return c};c.addDays=function(a,b){return c.addMilliseconds(a,864E5*b)};c.addHours=function(a,b){return c.addMilliseconds(a,36E5*b)};c.addMinutes= | ||
function(a,b){return c.addMilliseconds(a,6E4*b)};c.addSeconds=function(a,b){return c.addMilliseconds(a,1E3*b)};c.addMilliseconds=function(a,b){return new Date(a.getTime()+b)};c.subtract=function(a,b){var c=a.getTime()-b.getTime();return{toMilliseconds:function(){return c},toSeconds:function(){return c/1E3|0},toMinutes:function(){return c/6E4|0},toHours:function(){return c/36E5|0},toDays:function(){return c/864E5|0}}};c.isLeapYear=function(a){a=a.getFullYear();return!(a%4)&&!!(a%100)||!(a%400)};c.isSameDay= | ||
function(a,b){return c.format(a,"YYYYMMDD")===c.format(b,"YYYYMMDD")};c.locale=function(a){a&&("en"!==a&&r()&&require("./locale/"+a),n=a);return n};c.getLocales=function(a){return l[a||n]};c.setLocales=function(a,b){var c=function(a,c){var b=function(){},d;b.prototype=c;b=new b;for(d in a)a.hasOwnProperty(d)&&(b[d]=a[d]);return b},d=l[a]||l.en,e=c(b,d);b.formatter&&(e.formatter=c(b.formatter,d.formatter));b.parser&&(e.parser=c(b.parser,d.parser));l[a]=e};r()?module.exports=c:"function"===typeof define&& | ||
define.amd?define([],function(){return c}):t.date=c})(this); |
@@ -20,3 +20,3 @@ /** | ||
A: ['ص', 'م'], | ||
formats: { | ||
formatter: { | ||
post: function (str) { | ||
@@ -28,3 +28,3 @@ return str.replace(/\d/g, function (i) { | ||
}, | ||
parsers: { | ||
parser: { | ||
pre: function (str) { | ||
@@ -48,2 +48,1 @@ return str.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (i) { | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['রাত', 'সকাল', 'দুপুর', 'বিকাল'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -33,3 +33,3 @@ var h = d.getHours(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
@@ -58,2 +58,1 @@ if (a < 1) { | ||
}(this)); | ||
@@ -29,2 +29,1 @@ /** | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['de la mañana', 'de la tarde', 'de la noche'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -29,6 +29,6 @@ var h = d.getHours(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
if (a < 1) { // de la mañana | ||
return h; | ||
if (a < 1) { | ||
return h; // de la mañana | ||
} | ||
@@ -50,2 +50,1 @@ return h > 11 ? h : h + 12; // de la tarde, de la noche | ||
}(this)); | ||
@@ -29,2 +29,1 @@ /** | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['रात', 'सुबह', 'दोपहर', 'शाम'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -33,3 +33,3 @@ var h = d.getHours(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
@@ -58,2 +58,1 @@ if (a < 1) { | ||
}(this)); | ||
@@ -29,2 +29,1 @@ /** | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['午前', '午後'], | ||
formats: { | ||
formatter: { | ||
hh: function (d) { | ||
@@ -38,2 +38,1 @@ return ('0' + d.getHours() % 12).slice(-2); | ||
}(this)); | ||
@@ -29,2 +29,1 @@ /** | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['da madrugada', 'da manhã', 'da tarde', 'da noite'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -31,6 +31,6 @@ var h = d.getHours(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
if (a < 2) { // da madrugada, da manhã | ||
return h; | ||
if (a < 2) { | ||
return h; // da madrugada, da manhã | ||
} | ||
@@ -52,2 +52,1 @@ return h > 11 ? h : h + 12; // da tarde, da noite | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['ночи', 'утра', 'дня', 'вечера'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -31,6 +31,6 @@ var h = d.getHours(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
if (a < 2) { // ночи, утра | ||
return h; | ||
if (a < 2) { | ||
return h; // ночи, утра | ||
} | ||
@@ -52,2 +52,1 @@ return h > 11 ? h : h + 12; // дня, вечера | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['凌晨', '早上', '上午', '中午', '下午', '晚上'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -35,6 +35,6 @@ var hm = d.getHours() * 100 + d.getMinutes(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
if (a < 4) { // 凌晨, 早上, 上午, 中午 | ||
return h; | ||
if (a < 4) { | ||
return h; // 凌晨, 早上, 上午, 中午 | ||
} | ||
@@ -56,2 +56,1 @@ return h > 11 ? h : h + 12; // 下午, 晚上 | ||
}(this)); | ||
@@ -17,3 +17,3 @@ /** | ||
A: ['早上', '上午', '中午', '下午', '晚上'], | ||
formats: { | ||
formatter: { | ||
A: function (d) { | ||
@@ -33,6 +33,6 @@ var hm = d.getHours() * 100 + d.getMinutes(); | ||
}, | ||
parsers: { | ||
parser: { | ||
h: function (h, a) { | ||
if (a < 3) { // 早上, 上午, 中午 | ||
return h; | ||
if (a < 3) { | ||
return h; // 早上, 上午, 中午 | ||
} | ||
@@ -54,2 +54,1 @@ return h > 11 ? h : h + 12; // 下午, 晚上 | ||
}(this)); | ||
{ | ||
"name": "date-and-time", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A Minimalist DateTime utility for Node.js and the browser", | ||
@@ -8,3 +8,3 @@ "main": "date-and-time.js", | ||
"test": "./test.sh", | ||
"compile": "./compile.sh" | ||
"compile": "./compile.sh date-and-time.js date-and-time.min.js" | ||
}, | ||
@@ -16,4 +16,2 @@ "repository": { | ||
"keywords": [ | ||
"date-time", | ||
"datetime", | ||
"date", | ||
@@ -33,3 +31,3 @@ "time", | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.3.4", | ||
"mocha": "^2.5.3", | ||
"mocha-phantomjs": "^3.6.0", | ||
@@ -36,0 +34,0 @@ "phantomjs": "1.9.7-15" |
118
README.md
@@ -5,9 +5,10 @@ # date-and-time | ||
## WHY | ||
Probably the most famous DateTime library is [Moment.js](http://momentjs.com/), that is so great but has been bloated (13.8k gz). This will be a good solution if you require a small one. | ||
[Moment.js](http://momentjs.com/), the most famous DateTime utility, is very useful. But it's also the bloated module (**20.7k** gz). If you are looking for a similar and smaller one, this would be a good solution. | ||
## Features | ||
- Minimalist. only has 1.8k gz | ||
- multi language support | ||
- not extending built-in date object | ||
- legacy IE support. IE6+ | ||
- Minimalist. Only has **1.9k** (minified and gzipped) | ||
- Universal (Isomorphic) | ||
- Multi language support | ||
- Not extending built-in Date object | ||
- Legacy IE support. IE6+ | ||
@@ -23,3 +24,3 @@ ## Installation | ||
``` | ||
the browser (directly): | ||
directly: | ||
```html | ||
@@ -44,8 +45,15 @@ <script src="date-and-time.min.js"></script> | ||
## Breaking changes in 0.4.0 | ||
- `parse` | ||
- It comes to return a `NaN` object in case of parse error. | ||
- If year is not supplied in a date string, the default year of the returning date object is `1970`. | ||
- `locale` | ||
- Slightly changed the internal structure. | ||
## API | ||
### format(dateObj, formatString[, utc]) | ||
- {object} dateObj - target date | ||
- {string} formatString - format string | ||
- {boolean} [utc] - output as UTC | ||
- {object} **dateObj** - date object | ||
- {string} **formatString** - format string | ||
- {boolean} [**utc**] - output as UTC *(optional)* | ||
@@ -60,3 +68,3 @@ ```javascript | ||
| formatString | meaning | example | | ||
| token | meaning | example | | ||
|:-------------|:------------|:------------------| | ||
@@ -97,5 +105,5 @@ | YYYY | year | 0999, 2015 | | ||
### parse(dateString, formatString[, utc]) | ||
- {string} dateString - date string | ||
- {string} formatString - format string | ||
- {boolean} [utc] - input as UTC | ||
- {string} **dateString** - date string | ||
- {string} **formatString** - format string | ||
- {boolean} [**utc**] - input as UTC *(optional)* | ||
@@ -105,7 +113,9 @@ ```javascript | ||
date.parse('02-01-2015', 'DD-MM-YYYY'); // => date object | ||
date.parse('11:14:05 p.m.', 'hh:mm:ss A'); // => (23:14:05 GMT-0800) | ||
date.parse('11:14:05 p.m.', 'hh:mm:ss A', true); // => (15:14:05 GMT-0800) | ||
date.parse('11:14:05 p.m.', 'hh:mm:ss A'); // => (Jan 1 1970 23:14:05 GMT-0800) | ||
date.parse('11:14:05 p.m.', 'hh:mm:ss A', true); // => (Jan 1 1970 15:14:05 GMT-0800) | ||
date.parse('Jam 1 2017', 'MMM D YYYY'); // => NaN | ||
date.parse('Feb 29 2017', 'MMM D YYYY'); // => NaN | ||
``` | ||
| formatString | meaning | example | | ||
| token | meaning | example | | ||
|:-------------|:------------|:------------------| | ||
@@ -148,13 +158,13 @@ | YYYY | year | 2015, 1999 | | ||
### isValid(dateString, formatString) | ||
- {string} dateString - date string | ||
- {string} formatString - format string | ||
- {string} **dateString** - date string | ||
- {string} **formatString** - format string | ||
```javascript | ||
date.isValid('2015/01/02 23:14:05', 'YYYY/MM/DD HH:mm:ss'); // => true | ||
date.isValid('02-29-2015', 'DD-MM-YYYY'); // => false | ||
date.isValid('29-02-2015', 'DD-MM-YYYY'); // => false | ||
``` | ||
`formatString` is the same as one of `parse`. | ||
The `formatString` you can set is the same as the `parse` function's. | ||
### addYears(dateObj, years) | ||
- {object} dateObj - the augend | ||
- {number} years - the addend | ||
- {object} **dateObj** - date object | ||
- {number} **years** - adding year | ||
```javascript | ||
@@ -166,4 +176,4 @@ var now = new Date(); | ||
### addMonths(dateObj, months) | ||
- {object} dateObj - the augend | ||
- {number} months - the addend | ||
- {object} **dateObj** - date object | ||
- {number} **months** - adding month | ||
```javascript | ||
@@ -175,4 +185,4 @@ var now = new Date(); | ||
### addDays(dateObj, days) | ||
- {object} dateObj - the augend | ||
- {number} days - the addend | ||
- {object} **dateObj** - date object | ||
- {number} **days** - adding day | ||
```javascript | ||
@@ -184,4 +194,4 @@ var now = new Date(); | ||
### addHours(dateObj, hours) | ||
- {object} dateObj - the augend | ||
- {number} hours - the addend | ||
- {object} **dateObj** - date object | ||
- {number} **hours** - adding hour | ||
```javascript | ||
@@ -193,4 +203,4 @@ var now = new Date(); | ||
### addMinutes(dateObj, minutes) | ||
- {object} dateObj - the augend | ||
- {number} minutes - the addend | ||
- {object} **dateObj** - date object | ||
- {number} **minutes** - adding minute | ||
```javascript | ||
@@ -202,4 +212,4 @@ var now = new Date(); | ||
### addSeconds(dateObj, seconds) | ||
- {object} dateObj - the augend | ||
- {number} seconds - the addend | ||
- {object} **dateObj** - date object | ||
- {number} **seconds** - adding second | ||
```javascript | ||
@@ -211,4 +221,4 @@ var now = new Date(); | ||
### addMilliseconds(dateObj, milliseconds) | ||
- {object} dateObj - the augend | ||
- {number} milliseconds -the addend | ||
- {object} **dateObj** - date object | ||
- {number} **milliseconds** - adding millisecond | ||
```javascript | ||
@@ -220,8 +230,9 @@ var now = new Date(); | ||
### subtract(dateObj1, dateObj2) | ||
- {object} date1 - the minuend | ||
- {object} date2 - the subtrahend | ||
- {object} **date1** - date object | ||
- {object} **date2** - date object | ||
```javascript | ||
var today = new Date(2015, 0, 2); | ||
var yesterday = new Date(2015, 0, 1); | ||
date.subtract(today, yesterday).toDays(); // => 1 | ||
date.subtract(today, yesterday).toDays(); // => 1 = today - yesterday | ||
date.subtract(today, yesterday).toHours(); // => 24 | ||
@@ -234,3 +245,3 @@ date.subtract(today, yesterday).toMinutes(); // => 1440 | ||
### isLeapYear(dateObj) | ||
- {object} dateObj - target date | ||
- {object} **dateObj** - date object | ||
```javascript | ||
@@ -243,20 +254,49 @@ var date1 = new Date(2015, 0, 2); | ||
### isSameDay(dateObj) | ||
- {object} **date1** - date object | ||
- {object} **date2** - date object | ||
```javascript | ||
var date1 = new Date(2017, 0, 2, 0); // Jan 2 2017 00:00:00 | ||
var date2 = new Date(2017, 0, 2, 23, 59); // Jan 2 2017 23:59:00 | ||
var date3 = new Date(2017, 0, 1, 23, 59); // Jan 1 2017 23:59:00 | ||
date.isSameDay(date1, date2); // => true | ||
date.isSameDay(date1, date3); // => false | ||
``` | ||
## Locale | ||
It supports the following languages for now: | ||
- Arabic (ar) | ||
- Azerbaijani (az) | ||
- Bengali (bn) | ||
- Burmese (my) | ||
- Chinese (zh-cn) | ||
- Chinese (zh-tw) | ||
- Czech (cs) | ||
- Dutch (nl) | ||
- English (en) | ||
- French (fr) | ||
- German (de) | ||
- Greek (el) | ||
- Hindi (hi) | ||
- Hungarian (hu) | ||
- Indonesian (id) | ||
- Italian (it) | ||
- Japanese (ja) | ||
- Javanese (jv) | ||
- Korean (ko) | ||
- Persian (fa) | ||
- Polish (pl) | ||
- Portuguese (pt) | ||
- Punjabi (pa-in) | ||
- Romanian (ro) | ||
- Russian (ru) | ||
- Serbian (sr) | ||
- Spanish (es) | ||
- Thai (th) | ||
- Turkish (tr) | ||
- Ukrainian (uk) | ||
- Uzbek (uz) | ||
- Vietnamese (vi) | ||
Month, day of week, and meridiem are used English by default. If you want to use other languages, can switch to them as follows: | ||
Month, day of week, and meridiem are displayed in English by default. If you want to use other languages, can switch to them as follows: | ||
Node.js: | ||
@@ -263,0 +303,0 @@ ```javascript |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84990
36
1517
328