date-format-lite
Advanced tools
Comparing version 0.8.0 to 0.9.0
/*! litejs.com/MIT-LICENSE.txt */ | ||
!function(f,g){var p=/("|')((?:\\?.)*?)\1|([YMD])\3\3\3?|([YMDHhmsWSZ])(\4?)|[uUASwoQ]/g,q=/(\d+)[-.\/](\d+)[-.\/](\d+)/,r=/(\d+):(\d+)(?::(\d+))?(\.\d+)?(?:\s*(?:(a)|(p))\.?m\.?)?(\s*(?:Z|GMT|UTC)?(?:([-+]\d\d):?(\d\d)?)?)?/i,t=/\\(.)/g,u={D:"Date",h:"Hours",m:"Minutes",s:"Seconds",S:"Milliseconds"};f[g].date=f[g].format=function(b,c){b=f.masks[b]||b||f.masks["default"];var n,a=this,k=+a,d="get"+("UTC:"==b.slice(0,4)?(b=b.slice(4),"UTC"):""),e=void 0==c?a._z:c;void 0!=e&&"get"==d&&(d="getUTC",a.setTime(k+ | ||
36E5*e),n=60*e);b=b.replace(p,function(b,h,c,e,l,g){c="Y"==e?a[d+"FullYear"]():e?f.names[a[d+("M"==e?"Month":"Day")]()+("DDD"==b?24:"D"==e?31:"MMM"==b?0:12)]:"Y"==l?a[d+"FullYear"]()%100:"W"==l?(h=new f(k+864E5*(4-(a[d+"Day"]()||7))),Math.ceil(((h.getTime()-h["s"+d.slice(1)+"Month"](0,1))/864E5+1)/7)):"M"==l?a[d+"Month"]()+1:"H"==l?a[d+"Hours"]()%12||12:"Z"==l?(h=n||"get"==d&&-a.getTimezoneOffset()||0,h?(0>h?(h=-h,"-"):"+")+(600>h?"0":"")+(0|h/60)+((h%=60)?(g?"":":")+h:""):"Z"):l?a[d+u[l]]():"u"== | ||
b?a/1E3>>>0:"U"==b?k:"Q"==b?(a[d+"Month"]()/3|0)+1:"A"==b?f[11<a[d+"Hours"]()?"pm":"am"]:"w"==b?a[d+"Day"]()||7:"o"==b?(new f(k+864E5*(4-(a[d+"Day"]()||7))))[d+"FullYear"]():h?c.replace(t,"$1"):b;"SS"==b&&100>c&&(c="0"+c);return g&&10>c&&"Z"!=l?"0"+c:c});void 0!=n&&a.setTime(k);return b};f[g].tz=function(b){this._z=b;return this};f[g].add=function(b,c){b|=0;"months"==c?this.setUTCMonth(this.getUTCMonth()+b):"years"==c?this.setFullYear(this.getFullYear()+b):this.setTime(this.getTime()+b*("days"==c? | ||
864E5:"hours"==c?36E5:"minutes"==c?6E4:"seconds"==c?1E3:"weeks"==c?6048E5:1));return this};f.am="AM";f.pm="PM";f.masks={"default":"DDD MMM DD YYYY hh:mm:ss",iso:"UTC:YYYY-MM-DD'T'hh:mm:ss'Z'"};f.names="JanFebMarAprMayJunJulAugSepOctNovDecJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberSunMonTueWedThuFriSatSundayMondayTuesdayWednesdayThursdayFridaySaturday".match(/.[a-z]+/g);String[g].date=Number[g].date=function(b,c,g){var a,k,d,e=new f,m=+this||""+this;if(isNaN(m)){if(a= | ||
m.match(q))k=99<a[1]?1:3,d=f.middleEndian?4-k:2,e.setFullYear(a[k],a[d]-1,a[6-d-k]);a=m.match(r)||[0,0,0];e.setHours(a[6]&&12>a[1]?+a[1]+12:a[5]&&12==a[1]?0:a[1],a[2],a[3]|0,1E3*a[4]|0);a[7]&&(g=(a[8]|0)+(a[9]|0)/(0>a[8]?-60:60))}else e.setTime(4294967296>m?1E3*m:m);void 0!=g&&e.setTime(e-6E4*(60*g+e.getTimezoneOffset()));return b?e.format(b,c):e}}(Date,"prototype"); | ||
!function(c,f){var p=/("|')((?:\\?.)*?)\1|([YMD])\3\3\3?|([YMDHhmsWSZ])(\4?)|[uUASwoQ]/g,q=/(\d+)[-.\/](\d+)[-.\/](\d+)/,r=/(\d+):(\d+)(?::(\d+))?(\.\d+)?(?:\s*(?:(a)|(p))\.?m\.?)?(\s*(?:Z|GMT|UTC)?(?:([-+]\d\d):?(\d\d)?)?)?/i,t=/\\(.)/g,u={D:"Date",h:"Hours",m:"Minutes",s:"Seconds",S:"Milliseconds"},h={days:864E5,hours:36E5,minutes:6E4,seconds:1E3,weeks:6048E5};c[f].date=c[f].format=function(a,m){a=c.masks[a]||a||c.masks["default"];var f,b=this,k=+b,e="get"+("UTC:"==a.slice(0,4)?(a=a.slice(4),"UTC"): | ||
""),d=void 0==m?b._z:m;void 0!=d&&"get"==e&&(e="getUTC",b.setTime(k+36E5*d),f=60*d);a=a.replace(p,function(a,g,d,m,l,h){d="Y"==m?b[e+"FullYear"]():m?c.names[b[e+("M"==m?"Month":"Day")]()+("DDD"==a?24:"D"==m?31:"MMM"==a?0:12)]:"Y"==l?b[e+"FullYear"]()%100:"W"==l?(g=new c(k+864E5*(4-(b[e+"Day"]()||7))),Math.ceil(((g.getTime()-g["s"+e.slice(1)+"Month"](0,1))/864E5+1)/7)):"M"==l?b[e+"Month"]()+1:"H"==l?b[e+"Hours"]()%12||12:"Z"==l?(g=f||"get"==e&&-b.getTimezoneOffset()||0,g?(0>g?(g=-g,"-"):"+")+(600> | ||
g?"0":"")+(0|g/60)+((g%=60)?(h?"":":")+g:""):"Z"):l?b[e+u[l]]():"u"==a?b/1E3>>>0:"U"==a?k:"Q"==a?(b[e+"Month"]()/3|0)+1:"A"==a?c[11<b[e+"Hours"]()?"pm":"am"]:"w"==a?b[e+"Day"]()||7:"o"==a?(new c(k+864E5*(4-(b[e+"Day"]()||7))))[e+"FullYear"]():g?d.replace(t,"$1"):a;"SS"==a&&100>d&&(d="0"+d);return h&&10>d&&"Z"!=l?"0"+d:d});void 0!=f&&b.setTime(k);return a};c[f].tz=function(a){this._z=a;return this};c[f].add=function(a,c){a|=0;"months"==c||"years"==c&&(a*=12)?this.setUTCMonth(this.getUTCMonth()+a): | ||
this.setTime(this.getTime()+a*(h[c]||1));return this};c[f].startOf=function(a){this.setTime(this-this%(h[a]||1));return this};c[f].endOf=function(a){return this.startOf(a).add(1,a).add(-1)};c[f].diff=function(a,c){return(this-a)/(h[c]||1)|0};c.am="AM";c.pm="PM";c.masks={"default":"DDD MMM DD YYYY hh:mm:ss",iso:"UTC:YYYY-MM-DD'T'hh:mm:ss'Z'"};c.names="JanFebMarAprMayJunJulAugSepOctNovDecJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberSunMonTueWedThuFriSatSundayMondayTuesdayWednesdayThursdayFridaySaturday".match(/.[a-z]+/g); | ||
String[f].date=Number[f].date=function(a,f,h){var b,k,e,d=new c,n=+this||""+this;if(isNaN(n)){if(b=n.match(q))k=99<b[1]?1:3,e=c.middleEndian?4-k:2,d.setFullYear(b[k],b[e]-1,b[6-e-k]);b=n.match(r)||[0,0,0];d.setHours(b[6]&&12>b[1]?+b[1]+12:b[5]&&12==b[1]?0:b[1],b[2],b[3]|0,1E3*b[4]|0);b[7]&&(h=(b[8]|0)+(b[9]|0)/(0>b[8]?-60:60))}else d.setTime(4294967296>n?1E3*n:n);void 0!=h&&d.setTime(d-6E4*(60*h+d.getTimezoneOffset()));return a?d.format(a,f):d}}(Date,"prototype"); |
54
index.js
@@ -5,3 +5,3 @@ | ||
/* | ||
* @version 0.8.0 | ||
* @version 0.9.0 | ||
* @author Lauri Rooden <lauri@rooden.ee> | ||
@@ -19,2 +19,10 @@ * @license MIT License | ||
, map = { D:"Date", h:"Hours", m:"Minutes", s:"Seconds", S:"Milliseconds" } | ||
, units = { | ||
days: 86400000, | ||
hours: 3600000, | ||
minutes: 60000, | ||
seconds: 1000, | ||
weeks: 604800000 | ||
} | ||
//, isoDateRe = /(\d{4})[-.\/]W(\d\d?)[-.\/](\d)/ | ||
@@ -74,6 +82,10 @@ | ||
: match | ||
if (match == "SS" && text < 100) text = "0" + text | ||
if (match == "SS" && text < 100) { | ||
text = "0" + text | ||
} | ||
return pad && text < 10 && single != "Z" ? "0" + text : text | ||
}) | ||
if (zonediff != undef) date.setTime( origin ) | ||
if (zonediff != undef) { | ||
date.setTime(origin) | ||
} | ||
return mask | ||
@@ -90,15 +102,6 @@ } | ||
amount |= 0 | ||
if (unit == "months") { | ||
if (unit == "months" || unit == "years" && (amount *= 12)) { | ||
date.setUTCMonth(date.getUTCMonth() + amount) | ||
} else if (unit == "years") { | ||
date.setFullYear(date.getFullYear() + amount) | ||
} else { | ||
date.setTime(date.getTime() + (amount * ( | ||
unit == "days" ? 86400000 : | ||
unit == "hours" ? 3600000 : | ||
unit == "minutes" ? 60000 : | ||
unit == "seconds" ? 1000 : | ||
unit == "weeks" ? 604800000 : | ||
1 | ||
))) | ||
date.setTime(date.getTime() + (amount * (units[unit] || 1))) | ||
} | ||
@@ -108,2 +111,17 @@ return date | ||
Date[proto].startOf = function(unit) { | ||
var date = this | ||
date.setTime(date - (date % (units[unit] || 1))) | ||
return date | ||
} | ||
Date[proto].endOf = function(unit) { | ||
return this.startOf(unit).add(1, unit).add(-1) | ||
} | ||
Date[proto].diff = function(from, unit) { | ||
var diff = (this - from) / (units[unit] || 1) | ||
return diff | 0 | ||
} | ||
Date.am = "AM" | ||
@@ -153,5 +171,9 @@ Date.pm = "PM" | ||
} | ||
} else date.setTime( num < 4294967296 ? num * 1000 : num ) | ||
} else { | ||
date.setTime(num < 4294967296 ? num * 1000 : num) | ||
} | ||
if (zoneIn != undef) date.setTime(date - (60 * zoneIn + date.getTimezoneOffset()) * 60000) | ||
if (zoneIn != undef) { | ||
date.setTime(date - (60 * zoneIn + date.getTimezoneOffset()) * 60000) | ||
} | ||
@@ -158,0 +180,0 @@ return format ? date.format(format, zoneOut) : date |
{ | ||
"name": "date-format-lite", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"stability": 2, | ||
@@ -23,3 +23,3 @@ "author": "Lauri Rooden <lauri@rooden.ee>", | ||
"build": "node node_modules/buildman/index.js --all", | ||
"test": "node tests/index.js" | ||
"test": "env TZ='Europe/Tallinn' node tests/index.js" | ||
}, | ||
@@ -26,0 +26,0 @@ "repository": "git://github.com/litejs/date-format-lite.git", |
9993
159