Comparing version 0.1.1 to 0.2.0
@@ -7,14 +7,10 @@ if ( typeof module !== 'undefined' ) { | ||
if ( !(date instanceof Date) ) { | ||
if ( typeof date === 'string' ) { | ||
if ( /^\d\d\d\d\-\d\d\-\d\d$/.test(date) ) { | ||
d = date.split('-'); | ||
y = d[0]; | ||
m = d[1] - 1; | ||
d = d[2]; | ||
date = new Date(y, m, d); | ||
try { | ||
if ( typeof date === 'string' ) { | ||
date = new Date(Date.parse(date)); | ||
} else { | ||
date = new Date(Date.parse(date)); | ||
date = new Date(date) | ||
} | ||
} else { | ||
date = new Date(date); | ||
} catch(e) { | ||
throw Error(e); | ||
} | ||
@@ -27,4 +23,4 @@ } | ||
format.split('').forEach(function(let) { | ||
if ( let === '\\' && !escape ) { | ||
format.split('').forEach(function(letter) { | ||
if ( letter === '\\' && !escape ) { | ||
escape = true; | ||
@@ -35,3 +31,3 @@ return; | ||
if ( escape ) { | ||
output += let; | ||
output += letter; | ||
escape = false; | ||
@@ -41,3 +37,3 @@ return; | ||
output += _translate(let, date); | ||
output += _translate(letter, date); | ||
}); | ||
@@ -58,24 +54,16 @@ | ||
return short ? 'Sun' : 'Sunday'; | ||
break; | ||
case 1: | ||
return short ? 'Mon' : 'Monday'; | ||
break; | ||
case 2: | ||
return short ? 'Tue' : 'Tuesday'; | ||
break; | ||
case 3: | ||
return short ? 'Wed' : 'Wednesday'; | ||
break; | ||
case 4: | ||
return short ? 'Thu' : 'Thursday'; | ||
break; | ||
case 5: | ||
return short ? 'Fri' : 'Friday'; | ||
break; | ||
case 6: | ||
return short ? 'Sat' : 'Saturday'; | ||
break; | ||
default: | ||
return null; | ||
break; | ||
} | ||
@@ -89,39 +77,26 @@ }; | ||
return short ? 'Jan' : 'January'; | ||
break; | ||
case 1: | ||
return short ? 'Feb' : 'February'; | ||
break; | ||
case 2: | ||
return short ? 'Mar' : 'March'; | ||
break; | ||
case 3: | ||
return short ? 'Apr' : 'April'; | ||
break; | ||
case 4: | ||
return 'May'; | ||
break; | ||
case 5: | ||
return short ? 'Jun' : 'June'; | ||
break; | ||
case 6: | ||
return short ? 'Jul' : 'July'; | ||
break; | ||
case 7: | ||
return short ? 'Aug' : 'August'; | ||
break; | ||
case 8: | ||
return short ? 'Sept' : 'September'; | ||
break; | ||
case 9: | ||
return short ? 'Oct' : 'October'; | ||
break; | ||
case 10: | ||
return short ? 'Nov' : 'November'; | ||
break; | ||
case 11: | ||
return short ? 'Dec' : 'December'; | ||
break; | ||
default: | ||
return null; | ||
break; | ||
} | ||
@@ -135,3 +110,2 @@ }; | ||
return d < 10 ? '0' + d : d; | ||
break; | ||
@@ -141,3 +115,2 @@ // three-letter day | ||
return day(date, true); | ||
break; | ||
@@ -147,3 +120,2 @@ // day of month, no leading zeros | ||
return date.getDate(); | ||
break; | ||
@@ -153,3 +125,2 @@ // full word day | ||
return day(date, false); | ||
break; | ||
@@ -159,3 +130,2 @@ // numeric day of week, one-based | ||
return date.getDay() + 1; | ||
break; | ||
@@ -167,15 +137,10 @@ // suffix | ||
return 'st'; | ||
break; | ||
} else if ( d === 2 || ( /\d2$/.test(d.toString()) && !/1\d$/.test(d.toString()) ) ) { | ||
return 'nd'; | ||
break; | ||
} else if ( d === 3 || ( /\d3$/.test(d.toString()) && !/1\d$/.test(d.toString()) ) ) { | ||
return 'rd'; | ||
break; | ||
} else if ( /1\d|[4567890]/.test(d.toString()) ) { | ||
return 'th'; | ||
break; | ||
} else { | ||
return null; | ||
break; | ||
} | ||
@@ -186,3 +151,2 @@ | ||
return date.getDay(); | ||
break; | ||
@@ -195,3 +159,2 @@ // numeric day of the year | ||
return Math.ceil(diff / (1000 * 60 * 60 * 24)); | ||
break; | ||
@@ -201,3 +164,2 @@ // month, full word | ||
return month(date, false); | ||
break; | ||
@@ -208,3 +170,2 @@ // numeric month with leading zero | ||
return mo < 10 ? '0' + mo : mo; | ||
break; | ||
@@ -214,3 +175,2 @@ // month, abbreviated to three-letters | ||
return month(date, true); | ||
break; | ||
@@ -220,3 +180,2 @@ // numeric month without leading zero | ||
return date.getMonth() + 1; | ||
break; | ||
@@ -226,3 +185,2 @@ // four-digit year | ||
return date.getFullYear(); | ||
break; | ||
@@ -232,3 +190,2 @@ // two-digit year | ||
return date.getFullYear().toString().substr(2); | ||
break; | ||
@@ -238,3 +195,2 @@ // am or pm, lowercase | ||
return date.getHours() > 11 ? 'pm' : 'am'; | ||
break; | ||
@@ -244,3 +200,2 @@ // AM or PM, uppercase | ||
return date.getHours() > 11 ? 'PM' : 'AM'; | ||
break; | ||
@@ -250,3 +205,2 @@ // 12-hour format, no leading zero | ||
return date.getHours() % 12; | ||
break; | ||
@@ -256,3 +210,2 @@ // 24-hour format, no leading zero | ||
return date.getHours(); | ||
break; | ||
@@ -268,3 +221,2 @@ // 12-hour format, leading zero | ||
return h < 10 ? '0' + h : h; | ||
break; | ||
@@ -275,3 +227,2 @@ // minutes, with leading zero | ||
return m < 10 ? '0' + m : m; | ||
break; | ||
@@ -285,4 +236,3 @@ // seconds, with leading zero | ||
return letter; | ||
break; | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
function daylight(a,b){b instanceof Date||("string"==typeof b?/^\d\d\d\d\-\d\d\-\d\d$/.test(b)?(d=b.split("-"),y=d[0],m=d[1]-1,d=d[2],b=new Date(y,m,d)):b=new Date(Date.parse(b)):b=new Date(b));var c="",e=!1;return a.split("").forEach(function(a){return"\\"!==a||e?e?(c+=a,e=!1,void 0):(c+=_translate(a,b),void 0):(e=!0,void 0)}),c}function _translate(a,b){b instanceof Date||(b=new Date(b));var c=function(a,b){switch("undefined"==typeof b&&(b=!1),a.getDay()){case 0:return b?"Sun":"Sunday";case 1:return b?"Mon":"Monday";case 2:return b?"Tue":"Tuesday";case 3:return b?"Wed":"Wednesday";case 4:return b?"Thu":"Thursday";case 5:return b?"Fri":"Friday";case 6:return b?"Sat":"Saturday";default:return null}},d=function(a,b){switch("undefined"==typeof b&&(b=!1),a.getMonth()){case 0:return b?"Jan":"January";case 1:return b?"Feb":"February";case 2:return b?"Mar":"March";case 3:return b?"Apr":"April";case 4:return"May";case 5:return b?"Jun":"June";case 6:return b?"Jul":"July";case 7:return b?"Aug":"August";case 8:return b?"Sept":"September";case 9:return b?"Oct":"October";case 10:return b?"Nov":"November";case 11:return b?"Dec":"December";default:return null}};switch(a){case"d":var e=b.getDate();return 10>e?"0"+e:e;case"D":return c(b,!0);case"j":return b.getDate();case"l":return c(b,!1);case"N":return b.getDay()+1;case"S":var e=b.getDate();return 1===e?"st":2===e||/\d2$/.test(e.toString())&&!/1\d$/.test(e.toString())?"nd":3===e||/\d3$/.test(e.toString())&&!/1\d$/.test(e.toString())?"rd":/1\d|[4567890]/.test(e.toString())?"th":null;case"w":return b.getDay();case"z":var f=new Date(b.getFullYear(),0,0),g=b-f;return Math.ceil(g/864e5);case"F":return d(b,!1);case"m":var h=b.getMonth()+1;return 10>h?"0"+h:h;case"M":return d(b,!0);case"n":return b.getMonth()+1;case"Y":return b.getFullYear();case"y":return b.getFullYear().toString().substr(2);case"a":return b.getHours()>11?"pm":"am";case"A":return b.getHours()>11?"PM":"AM";case"g":return b.getHours()%12;case"G":return b.getHours();case"h":var i=b.getHours()%12;return 10>i?"0"+i:i;case"H":var i=b.getHours();return 10>i?"0"+i:i;case"i":var j=b.getMinutes();return 10>j?"0"+j:j;case"s":var k=b.getSeconds();return 10>k?"0"+k:k;default:return a}}"undefined"!=typeof module&&(module.exports=daylight); | ||
function daylight(a,b){if(!(b instanceof Date))try{b="string"==typeof b?new Date(Date.parse(b)):new Date(b)}catch(c){throw Error(c)}var d="",e=!1;return a.split("").forEach(function(a){return"\\"!==a||e?e?(d+=a,e=!1,void 0):(d+=_translate(a,b),void 0):(e=!0,void 0)}),d}function _translate(a,b){b instanceof Date||(b=new Date(b));var c=function(a,b){switch("undefined"==typeof b&&(b=!1),a.getDay()){case 0:return b?"Sun":"Sunday";case 1:return b?"Mon":"Monday";case 2:return b?"Tue":"Tuesday";case 3:return b?"Wed":"Wednesday";case 4:return b?"Thu":"Thursday";case 5:return b?"Fri":"Friday";case 6:return b?"Sat":"Saturday";default:return null}},d=function(a,b){switch("undefined"==typeof b&&(b=!1),a.getMonth()){case 0:return b?"Jan":"January";case 1:return b?"Feb":"February";case 2:return b?"Mar":"March";case 3:return b?"Apr":"April";case 4:return"May";case 5:return b?"Jun":"June";case 6:return b?"Jul":"July";case 7:return b?"Aug":"August";case 8:return b?"Sept":"September";case 9:return b?"Oct":"October";case 10:return b?"Nov":"November";case 11:return b?"Dec":"December";default:return null}};switch(a){case"d":var e=b.getDate();return 10>e?"0"+e:e;case"D":return c(b,!0);case"j":return b.getDate();case"l":return c(b,!1);case"N":return b.getDay()+1;case"S":var e=b.getDate();return 1===e?"st":2===e||/\d2$/.test(e.toString())&&!/1\d$/.test(e.toString())?"nd":3===e||/\d3$/.test(e.toString())&&!/1\d$/.test(e.toString())?"rd":/1\d|[4567890]/.test(e.toString())?"th":null;case"w":return b.getDay();case"z":var f=new Date(b.getFullYear(),0,0),g=b-f;return Math.ceil(g/864e5);case"F":return d(b,!1);case"m":var h=b.getMonth()+1;return 10>h?"0"+h:h;case"M":return d(b,!0);case"n":return b.getMonth()+1;case"Y":return b.getFullYear();case"y":return b.getFullYear().toString().substr(2);case"a":return b.getHours()>11?"pm":"am";case"A":return b.getHours()>11?"PM":"AM";case"g":return b.getHours()%12;case"G":return b.getHours();case"h":var i=b.getHours()%12;return 10>i?"0"+i:i;case"H":var i=b.getHours();return 10>i?"0"+i:i;case"i":var j=b.getMinutes();return 10>j?"0"+j:j;case"s":var k=b.getSeconds();return 10>k?"0"+k:k;default:return a}}"undefined"!=typeof module&&(module.exports=daylight); |
{ | ||
"name": "daylight", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "lightweight javascript date formatting", | ||
"main": "daylight.js", | ||
"dependencies": { | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tape": "^2.13.1" | ||
}, | ||
"devDependencies": {}, | ||
"testling": { | ||
"files": "test.js", | ||
"browsers": [ | ||
"ie/6..latest", | ||
"chrome/22..latest", | ||
"firefox/16..latest", | ||
"safari/latest", | ||
"opera/11.0..latest", | ||
"iphone/latest", | ||
"ipad/latest", | ||
"android-browser/latest" | ||
] | ||
}, | ||
"scripts": { | ||
@@ -11,0 +25,0 @@ "test": "node test.js" |
@@ -7,5 +7,8 @@ # daylight | ||
note: **daylight** does a rather rudimentary job parsing dates (strings are just passed through [`Date.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)), so beware of local timezone vs utc time-string pitfalls. | ||
### in the browser | ||
**TODO**: requirejs, commonjs, etc. module support | ||
```html | ||
<script src="daylight.js"></script> | ||
@@ -12,0 +15,0 @@ <script> |
44
test.js
var test = require('tape'); | ||
var formatDate = require('./daylight'); | ||
var date = '2014-05-28T09:00:00-04:00'; | ||
var date = '2014-05-28 09:00:00'; | ||
test('formatDate works', function(t) { | ||
t.equal(formatDate('l, F dS \'y', date), 'Wednesday, May 28th \'14'); | ||
t.equal(formatDate('l, F dS Y', date), 'Wednesday, May 28th 2014'); | ||
t.end(); | ||
}); | ||
test('date format: YYYY-MM-DD HH:MM:SS', function(t) { | ||
t.equal(formatDate('Y-m-d H:i:s', '2014-05-28 23:00:00'), '2014-05-28 23:00:00'); | ||
t.end(); | ||
}); | ||
test('date format: YYYY-MM-DDTHH:MM:SS (UTC timezone)', function(t) { | ||
t.equal(formatDate('Y-m-d\\TH:i:s', '2014-05-28T23:00:00'), '2014-05-28T19:00:00'); | ||
t.end(); | ||
}); | ||
test('escaping escapes', function(t) { | ||
@@ -19,3 +29,3 @@ t.equal(formatDate('\\M', date), 'M'); | ||
t.equal(formatDate('d', date), '28'); | ||
t.equal(formatDate('d', '2014-05-01'), '01'); | ||
t.equal(formatDate('d', '2014-05-01 00:00:00'), '01'); | ||
@@ -45,4 +55,4 @@ t.end(); | ||
t.equal(formatDate('N', date), '4'); | ||
t.equal(formatDate('N', '2014-05-24'), '7'); | ||
t.equal(formatDate('N', '2014-05-25'), '1'); | ||
t.equal(formatDate('N', '2014-05-24 00:00:00'), '7'); | ||
t.equal(formatDate('N', '2014-05-25 00:00:00'), '1'); | ||
@@ -56,8 +66,8 @@ t.end(); | ||
t.equal(formatDate('jS', '05-02-2014'), '2nd'); | ||
t.equal(formatDate('jS', '2014-05-03'), '3rd'); | ||
t.equal(formatDate('jS', '2014-05-04'), '4th'); | ||
t.equal(formatDate('jS', '2014-05-10'), '10th'); | ||
t.equal(formatDate('jS', '2014-05-11'), '11th'); | ||
t.equal(formatDate('jS', '2014-05-12'), '12th'); | ||
t.equal(formatDate('jS', '2014-05-13'), '13th'); | ||
t.equal(formatDate('jS', '2014-05-03 00:00:00'), '3rd'); | ||
t.equal(formatDate('jS', '2014-05-04 00:00:00'), '4th'); | ||
t.equal(formatDate('jS', '2014-05-10 00:00:00'), '10th'); | ||
t.equal(formatDate('jS', '2014-05-11 00:00:00'), '11th'); | ||
t.equal(formatDate('jS', '2014-05-12 00:00:00'), '12th'); | ||
t.equal(formatDate('jS', '2014-05-13 00:00:00'), '13th'); | ||
@@ -69,4 +79,4 @@ t.end(); | ||
t.equal(formatDate('w', date), '3'); | ||
t.equal(formatDate('w', '2014-05-24'), '6'); | ||
t.equal(formatDate('w', '2014-05-25'), '0'); | ||
t.equal(formatDate('w', '2014-05-24 00:00:00'), '6'); | ||
t.equal(formatDate('w', '2014-05-25 00:00:00'), '0'); | ||
@@ -77,4 +87,4 @@ t.end(); | ||
test('numeric day of the year: z', function(t) { | ||
t.equal(formatDate('z', '2014-01-01'), '1'); | ||
t.equal(formatDate('z', '2014-02-03'), '34'); | ||
t.equal(formatDate('z', '2014-01-01 00:00:00'), '1'); | ||
t.equal(formatDate('z', '2014-02-03 00:00:00'), '34'); | ||
@@ -98,3 +108,3 @@ t.end(); | ||
t.equal(formatDate('M', date), 'May'); | ||
t.equal(formatDate('M', '2014-08-01'), 'Aug'); | ||
t.equal(formatDate('M', '2014-08-01 00:00:00'), 'Aug'); | ||
@@ -176,2 +186,2 @@ t.end(); | ||
t.end(); | ||
}); | ||
}); |
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
0
162
18078
1
310
- Removedtape@^2.13.1
- Removeddeep-equal@0.2.2(transitive)
- Removeddefined@0.0.0(transitive)
- Removedglob@3.2.11(transitive)
- Removedhas@0.0.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.3.0(transitive)
- Removedobject-inspect@1.13.2(transitive)
- Removedresumer@0.0.0(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedtape@2.14.1(transitive)
- Removedthrough@2.3.8(transitive)