Comparing version 1.0.0 to 1.1.0
13
fecha.js
@@ -54,3 +54,6 @@ (function (main) { | ||
a: [word, function (d, v) { | ||
if (amPm.indexOf(v.toLowerCase())) { | ||
var val = v.toLowerCase(); | ||
if (val === amPm[0]) { | ||
d.isPm = false; | ||
} else if (val === amPm[1]) { | ||
d.isPm = true; | ||
@@ -232,7 +235,9 @@ } | ||
today = new Date(); | ||
if (dateInfo.isPm && dateInfo.hour) { | ||
dateInfo.hour = +dateInfo.hour + 12 | ||
if (dateInfo.isPm === true && dateInfo.hour != null && +dateInfo.hour !== 12) { | ||
dateInfo.hour = +dateInfo.hour + 12; | ||
} else if (dateInfo.isPm === false && +dateInfo.hour === 12) { | ||
dateInfo.hour = 0; | ||
} | ||
if (dateInfo.timezoneOffset) { | ||
if (dateInfo.timezoneOffset != null) { | ||
dateInfo.minute = +(dateInfo.minute || 0) - +dateInfo.timezoneOffset; | ||
@@ -239,0 +244,0 @@ date = new Date(Date.UTC(dateInfo.year || today.getFullYear(), dateInfo.month || 0, dateInfo.day || 1, |
@@ -1,1 +0,1 @@ | ||
!function(e){"use strict";function n(e){return function(n,t){var o=r.i18n[e].indexOf(t.charAt(0).toUpperCase()+t.substr(1).toLowerCase());~o&&(n.month=o)}}function t(e,n){for(e=String(e),n=n||2;e.length<n;)e="0"+e;return e}function o(e,n,t){for(var o=0,a=e.length;a>o;o++)n.push(e[o].substr(0,t))}function a(e){return e+["th","st","nd","rd"][e%10>3?0:(e-e%10!==10)*e%10]}var r={},u=/d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g,i=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],d=["January","February","March","April","May","June","July","August","September","October","November","December"],s=["am","pm"],m=/\d\d?/,f=/\d{3}/,h=/\d{4}/,c=/[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i,M=function(){},l=[],Y=[],D={D:[m,function(e,n){e.day=n}],M:[m,function(e,n){e.month=n-1}],YY:[m,function(e,n){var t=new Date,o=+(""+t.getFullYear()).substr(0,2);e.year=""+(n>68?o-1:o)+n}],h:[m,function(e,n){e.hour=n}],m:[m,function(e,n){e.minute=n}],s:[m,function(e,n){e.second=n}],YYYY:[h,function(e,n){e.year=n}],S:[/\d/,function(e,n){e.millisecond=100*n}],SS:[/\d{2}/,function(e,n){e.millisecond=10*n}],SSS:[f,function(e,n){e.millisecond=n}],d:[m,M],ddd:[c,M],MMM:[c,n("monthNamesShort")],MMMM:[c,n("monthNames")],a:[c,function(e,n){s.indexOf(n.toLowerCase())&&(e.isPm=!0)}],ZZ:[/[\+\-]\d\d:?\d\d/,function(e,n){var t,o=(n+"").match(/([\+\-]|\d\d)/gi);o&&(t=+(60*o[1])+parseInt(o[2],10),e.timezoneOffset="+"===o[0]?t:-t)}]};D.dd=D.d,D.dddd=D.ddd,D.Do=D.DD=D.D,D.mm=D.m,D.hh=D.H=D.HH=D.h,D.MM=D.M,D.ss=D.s,D.A=D.a,o(d,Y,3),o(i,l,3),r.i18n={dayNamesShort:l,dayNames:i,monthNamesShort:Y,monthNames:d,amPm:s,DoFn:a},r.masks={"default":"ddd MMM DD YYYY HH:mm:ss",shortDate:"M/D/YY",mediumDate:"MMM D, YYYY",longDate:"MMMM D, YYYY",fullDate:"dddd, MMMM D, YYYY",shortTime:"HH:mm",mediumTime:"HH:mm:ss",longTime:"HH:mm:ss.SSS"},r.format=function(e,n){if("number"==typeof e&&(e=new Date(e)),!e||"object"!=typeof e&&"function"!=typeof e.getDate)throw new Error("Invalid Date in fecha.format");n=r.masks[n]||n||r.masks["default"];var o=e.getDate(),a=e.getDay(),i=e.getMonth(),d=e.getFullYear(),s=e.getHours(),m=e.getMinutes(),f=e.getSeconds(),h=e.getMilliseconds(),c=e.getTimezoneOffset(),M={D:o,DD:t(o),Do:r.i18n.DoFn(o),d:a,dd:t(a),ddd:r.i18n.dayNamesShort[a],dddd:r.i18n.dayNames[a],M:i+1,MM:t(i+1),MMM:r.i18n.monthNamesShort[i],MMMM:r.i18n.monthNames[i],YY:String(d).slice(2),YYYY:d,h:s%12||12,hh:t(s%12||12),H:s,HH:t(s),m:m,mm:t(m),s:f,ss:t(f),S:Math.round(h/100),SS:t(Math.round(h/10),2),SSS:t(h,3),a:12>s?r.i18n.amPm[0]:r.i18n.amPm[1],A:12>s?r.i18n.amPm[0].toUpperCase():r.i18n.amPm[1].toUpperCase(),ZZ:(c>0?"-":"+")+t(100*Math.floor(Math.abs(c)/60)+Math.abs(c)%60,4)};return n.replace(u,function(e){return e in M?M[e]:e.slice(1,e.length-1)})},r.parse=function(e,n){var t,o,a,i,d,s;if("string"!=typeof n)throw new Error("Invalid format in fecha.parse");return n=r.masks[n]||n,t=!0,o={},n.replace(u,function(n){return D[n]&&(d=D[n],s=e.search(d[0]),~s?e.replace(d[0],function(n){return d[1](o,n),e=e.substr(s+n.length),n}):t=!1),D[n]?"":n.slice(1,n.length-1)}),t?(a=new Date,o.isPm&&o.hour&&(o.hour=+o.hour+12),o.timezoneOffset?(o.minute=+(o.minute||0)-+o.timezoneOffset,i=new Date(Date.UTC(o.year||a.getFullYear(),o.month||0,o.day||1,o.hour||0,o.minute||0,o.second||0,o.millisecond||0))):i=new Date(o.year||a.getFullYear(),o.month||0,o.day||1,o.hour||0,o.minute||0,o.second||0,o.millisecond||0),i):!1},"undefined"!=typeof module&&module.exports?module.exports=r:"function"==typeof define&&define.amd?define(function(){return r}):e.fecha=r}(this); | ||
!function(e){"use strict";function n(e){return function(n,t){var o=r.i18n[e].indexOf(t.charAt(0).toUpperCase()+t.substr(1).toLowerCase());~o&&(n.month=o)}}function t(e,n){for(e=String(e),n=n||2;e.length<n;)e="0"+e;return e}function o(e,n,t){for(var o=0,a=e.length;a>o;o++)n.push(e[o].substr(0,t))}function a(e){return e+["th","st","nd","rd"][e%10>3?0:(e-e%10!==10)*e%10]}var r={},u=/d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g,i=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],s=["January","February","March","April","May","June","July","August","September","October","November","December"],d=["am","pm"],m=/\d\d?/,f=/\d{3}/,h=/\d{4}/,c=/[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i,l=function(){},M=[],Y=[],D={D:[m,function(e,n){e.day=n}],M:[m,function(e,n){e.month=n-1}],YY:[m,function(e,n){var t=new Date,o=+(""+t.getFullYear()).substr(0,2);e.year=""+(n>68?o-1:o)+n}],h:[m,function(e,n){e.hour=n}],m:[m,function(e,n){e.minute=n}],s:[m,function(e,n){e.second=n}],YYYY:[h,function(e,n){e.year=n}],S:[/\d/,function(e,n){e.millisecond=100*n}],SS:[/\d{2}/,function(e,n){e.millisecond=10*n}],SSS:[f,function(e,n){e.millisecond=n}],d:[m,l],ddd:[c,l],MMM:[c,n("monthNamesShort")],MMMM:[c,n("monthNames")],a:[c,function(e,n){var t=n.toLowerCase();t===d[0]?e.isPm=!1:t===d[1]&&(e.isPm=!0)}],ZZ:[/[\+\-]\d\d:?\d\d/,function(e,n){var t,o=(n+"").match(/([\+\-]|\d\d)/gi);o&&(t=+(60*o[1])+parseInt(o[2],10),e.timezoneOffset="+"===o[0]?t:-t)}]};D.dd=D.d,D.dddd=D.ddd,D.Do=D.DD=D.D,D.mm=D.m,D.hh=D.H=D.HH=D.h,D.MM=D.M,D.ss=D.s,D.A=D.a,o(s,Y,3),o(i,M,3),r.i18n={dayNamesShort:M,dayNames:i,monthNamesShort:Y,monthNames:s,amPm:d,DoFn:a},r.masks={"default":"ddd MMM DD YYYY HH:mm:ss",shortDate:"M/D/YY",mediumDate:"MMM D, YYYY",longDate:"MMMM D, YYYY",fullDate:"dddd, MMMM D, YYYY",shortTime:"HH:mm",mediumTime:"HH:mm:ss",longTime:"HH:mm:ss.SSS"},r.format=function(e,n){if("number"==typeof e&&(e=new Date(e)),!e||"object"!=typeof e&&"function"!=typeof e.getDate)throw new Error("Invalid Date in fecha.format");n=r.masks[n]||n||r.masks["default"];var o=e.getDate(),a=e.getDay(),i=e.getMonth(),s=e.getFullYear(),d=e.getHours(),m=e.getMinutes(),f=e.getSeconds(),h=e.getMilliseconds(),c=e.getTimezoneOffset(),l={D:o,DD:t(o),Do:r.i18n.DoFn(o),d:a,dd:t(a),ddd:r.i18n.dayNamesShort[a],dddd:r.i18n.dayNames[a],M:i+1,MM:t(i+1),MMM:r.i18n.monthNamesShort[i],MMMM:r.i18n.monthNames[i],YY:String(s).slice(2),YYYY:s,h:d%12||12,hh:t(d%12||12),H:d,HH:t(d),m:m,mm:t(m),s:f,ss:t(f),S:Math.round(h/100),SS:t(Math.round(h/10),2),SSS:t(h,3),a:12>d?r.i18n.amPm[0]:r.i18n.amPm[1],A:12>d?r.i18n.amPm[0].toUpperCase():r.i18n.amPm[1].toUpperCase(),ZZ:(c>0?"-":"+")+t(100*Math.floor(Math.abs(c)/60)+Math.abs(c)%60,4)};return n.replace(u,function(e){return e in l?l[e]:e.slice(1,e.length-1)})},r.parse=function(e,n){var t,o,a,i,s,d;if("string"!=typeof n)throw new Error("Invalid format in fecha.parse");return n=r.masks[n]||n,t=!0,o={},n.replace(u,function(n){return D[n]&&(s=D[n],d=e.search(s[0]),~d?e.replace(s[0],function(n){return s[1](o,n),e=e.substr(d+n.length),n}):t=!1),D[n]?"":n.slice(1,n.length-1)}),t?(a=new Date,o.isPm===!0&&null!=o.hour&&12!==+o.hour?o.hour=+o.hour+12:o.isPm===!1&&12===+o.hour&&(o.hour=0),null!=o.timezoneOffset?(o.minute=+(o.minute||0)-+o.timezoneOffset,i=new Date(Date.UTC(o.year||a.getFullYear(),o.month||0,o.day||1,o.hour||0,o.minute||0,o.second||0,o.millisecond||0))):i=new Date(o.year||a.getFullYear(),o.month||0,o.day||1,o.hour||0,o.minute||0,o.second||0,o.millisecond||0),i):!1},"undefined"!=typeof module&&module.exports?module.exports=r:"function"==typeof define&&define.amd?define(function(){return r}):e.fecha=r}(this); |
@@ -42,11 +42,20 @@ (function () { | ||
}); | ||
it('ampm', function () { | ||
it('ampm 10PM', function () { | ||
expect(fecha.parse('2015-11-07 10PM', 'YYYY-MM-DD hhA')).toEqual(new Date(2015, 10, 7, 22)); | ||
}); | ||
it('ampm am', function () { | ||
expect(fecha.parse('2000-01-01 12AM', 'YYYY-MM-DD hhA')).toEqual(new Date(2000, 0, 1, 12)); | ||
it('ampm 9AM', function () { | ||
expect(fecha.parse('2015-11-07 9AM', 'YYYY-MM-DD hhA')).toEqual(new Date(2015, 10, 7, 9)); | ||
}); | ||
it('ampm 12am', function () { | ||
expect(fecha.parse('2000-01-01 12AM', 'YYYY-MM-DD hhA')).toEqual(new Date(2000, 0, 1, 0)); | ||
}); | ||
it('ampm 3am', function () { | ||
expect(fecha.parse('2000-01-01 3AM', 'YYYY-MM-DD hhA')).toEqual(new Date(2000, 0, 1, 3)); | ||
}); | ||
it('ampm am lowercase', function () { | ||
expect(fecha.parse('2000-01-01 11am', 'YYYY-MM-DD hha')).toEqual(new Date(2000, 0, 1, 11)); | ||
}); | ||
it('noon pm lowercase', function () { | ||
expect(fecha.parse('2000-01-01 12pm', 'YYYY-MM-DD hha')).toEqual(new Date(2000, 0, 1, 12)); | ||
}); | ||
it('24 hour time long', function () { | ||
@@ -74,2 +83,6 @@ expect(fecha.parse('2000-01-01 20', 'YYYY-MM-DD HH')).toEqual(new Date(2000, 0, 1, 20)); | ||
}); | ||
it('UTC timezone offset', function () { | ||
expect(fecha.parse('09:20:31 GMT-0000 (UTC)', 'HH:mm:ss ZZ')).toEqual(new Date(Date.UTC(2015, 0, 1, 9, | ||
20, 31))); | ||
}); | ||
it('invalid date', function () { | ||
@@ -181,2 +194,2 @@ expect(fecha.parse('hello', 'HH:mm:ss ZZ')).toEqual(false); | ||
}); | ||
})(); | ||
})(); |
{ | ||
"name": "fecha", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Date formatting and parsing", | ||
@@ -5,0 +5,0 @@ "main": "fecha.js", |
@@ -49,3 +49,9 @@ # fecha [![Build Status](https://travis-ci.org/taylorhakes/fecha.png?branch=master)](https://travis-ci.org/taylorhakes/fecha) | ||
#### Formatting | ||
`fecha.format` accepts a Date object (or timestamp) and a string format and returns a formatted string. See below for | ||
available format tokens. | ||
Note: `fecha.format` will throw an error when passed invalid parameters | ||
``` | ||
fecha.format(<Date Object>, <String Format>); | ||
// Custom formats | ||
@@ -62,2 +68,5 @@ fecha.format(new Date(2015, 10, 20), 'dddd MMMM Do, YYYY'); // 'Friday November 20th, 2015' | ||
#### Parsing | ||
`fecha.parse` accepts a Date string and a string format and returns a Date object. See below for available format tokens. | ||
Note: `fecha.parse` will throw an error when passed invalid parameters | ||
``` | ||
@@ -71,5 +80,2 @@ // Custom formats | ||
fecha.parse('November 4, 2005', 'longDate'); // new Date(2005, 10, 4) | ||
// DEPRECATED: Without Format (Can be unintuitive. Will be removed in future version. Not recommended) | ||
fecha.parse('10/03/2015'); // new Date(2015, 9, 3) | ||
``` | ||
@@ -76,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
508
257
48272
17