Comparing version
34
fecha.js
@@ -148,3 +148,12 @@ (function (main) { | ||
var D = dateObj.getDate(), d = dateObj.getDay(), M = dateObj.getMonth(), y = dateObj.getFullYear(), H = dateObj.getHours(), m = dateObj.getMinutes(), s = dateObj.getSeconds(), S = dateObj.getMilliseconds(), o = dateObj.getTimezoneOffset(), flags = { | ||
var D = dateObj.getDate(), | ||
d = dateObj.getDay(), | ||
M = dateObj.getMonth(), | ||
y = dateObj.getFullYear(), | ||
H = dateObj.getHours(), | ||
m = dateObj.getMinutes(), | ||
s = dateObj.getSeconds(), | ||
S = dateObj.getMilliseconds(), | ||
o = dateObj.getTimezoneOffset(), | ||
flags = { | ||
D: D, | ||
@@ -189,15 +198,24 @@ DD: pad(D), | ||
* @param {string} format Date parse format | ||
* @returns {Date} | ||
* @returns {Date|boolean} | ||
*/ | ||
fecha.parse = function (dateStr, format) { | ||
var time, isValid, dateInfo, today, date, info, index; | ||
if (!format) { | ||
return new Date(dateStr.replace(/\-/g, '/')); | ||
time = Date.parse(dateStr.replace(/\-/g, '/')); | ||
if (!isNaN(time)) { | ||
return new Date(time); | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
format = fecha.masks[format] || format; | ||
var isValid = true, dateInfo = {}; | ||
isValid = true; | ||
dateInfo = {}; | ||
format.replace(token, function ($0) { | ||
if (parseFlags[$0]) { | ||
var info = parseFlags[$0]; | ||
var index = dateStr.search(info[0]); | ||
info = parseFlags[$0]; | ||
index = dateStr.search(info[0]); | ||
if (!~index) { | ||
@@ -222,3 +240,3 @@ isValid = false; | ||
var today = new Date(), date; | ||
today = new Date(); | ||
if (dateInfo.isPm && dateInfo.hour) { | ||
@@ -241,3 +259,3 @@ dateInfo.hour = +dateInfo.hour + 12 | ||
module.exports = fecha; | ||
} else if (typeof require !== 'undefined' && require.amd) { | ||
} else if (typeof define === 'function' && define.amd) { | ||
define(function () { | ||
@@ -244,0 +262,0 @@ return fecha; |
@@ -73,2 +73,14 @@ (function () { | ||
}); | ||
it('invalid date', function () { | ||
expect(fecha.parse('hello', 'HH:mm:ss ZZ')).toEqual(false); | ||
}); | ||
it('invalid date no format', function () { | ||
expect(fecha.parse('hello')).toEqual(false); | ||
}); | ||
it('no format specified', function () { | ||
expect(fecha.parse('2014-11-05')).toEqual(new Date(2014, 10, 5)); | ||
}); | ||
it('another no format', function() { | ||
expect(fecha.parse('2015-02-29')).toEqual(new Date(2015, 1, 29)); | ||
}); | ||
}); | ||
@@ -75,0 +87,0 @@ describe('format', function () { |
@@ -16,3 +16,4 @@ // Karma configuration | ||
files: [ | ||
'*.js' | ||
'fecha.js', | ||
'fecha.spec.js' | ||
], | ||
@@ -19,0 +20,0 @@ |
{ | ||
"name": "fecha", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Date formatting and parsing", | ||
@@ -32,4 +32,7 @@ "main": "fecha.js", | ||
"karma-phantomjs-launcher": "^0.1.4", | ||
"karma-coverage": "^0.2.7" | ||
"karma-coverage": "^0.2.7", | ||
"gulp": "^3.8.10", | ||
"gulp-uglify": "^1.0.2", | ||
"gulp-rename": "^1.2.0" | ||
} | ||
} |
@@ -7,5 +7,41 @@ # fecha [](https://travis-ci.org/taylorhakes/fecha) | ||
``` | ||
npm install fecha --save-dev | ||
npm install fecha --save | ||
``` | ||
### Fecha vs Moment | ||
<table class="table table-striped table-bordered"> | ||
<tbody> | ||
<tr> | ||
<th></th> | ||
<th>Fecha</th> | ||
<th>Moment</th> | ||
</tr> | ||
<tr> | ||
<td><b>Size (Min. and Gzipped)</b></td> | ||
<td>2.1KBs</td> | ||
<td>13.1KBs</td> | ||
</tr> | ||
<tr> | ||
<td><b>Date Parsing</b></td> | ||
<td>✓</td> | ||
<td>✓</td> | ||
</tr> | ||
<tr> | ||
<td><b>Date Formatting</b></td> | ||
<td>✓</td> | ||
<td>✓</td> | ||
</tr> | ||
<tr> | ||
<td><b>Date Manipulation</b></td> | ||
<td>Native Only</td> | ||
<td>✓</td> | ||
</tr> | ||
<tr> | ||
<td><b>I18n Support</b></td> | ||
<td>✓</td> | ||
<td>✓</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
## Use it | ||
@@ -22,2 +58,3 @@ | ||
fecha.format(new Date(2015, 2, 10, 5, 30, 20), 'shortTime'); // '05:30' | ||
``` | ||
@@ -34,2 +71,5 @@ | ||
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) | ||
``` | ||
@@ -142,7 +182,2 @@ | ||
<tr> | ||
<td><b>Week Year</b></td> | ||
<td>gg</td> | ||
<td>70 71 ... 29 30</td> | ||
</tr> | ||
<tr> | ||
<td><b>AM/PM</b></td> | ||
@@ -149,0 +184,0 @@ <td>A</td> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70567
62.94%22
37.5%495
19.57%251
16.2%8
60%