Revision 02438e2c
Von Sven Schöling vor etwa 7 Jahren hinzugefügt
js/kivi.js | ||
---|---|---|
32 | 32 |
|
33 | 33 |
ns.parse_date = function(date) { |
34 | 34 |
var parts = date.replace(/\s+/g, "").split(ns._date_format.sep); |
35 |
date = new Date( |
|
36 |
((parts[ ns._date_format.y ] || 0) * 1) || (new Date()).getFullYear(), |
|
37 |
(parts[ ns._date_format.m ] || 0) * 1 - 1, // Months are 0-based. |
|
38 |
(parts[ ns._date_format.d ] || 0) * 1 |
|
39 |
); |
|
35 |
var today = new Date(); |
|
36 |
|
|
37 |
// without separator? |
|
38 |
// assume fixed pattern, and extract parts again |
|
39 |
if (parts.length == 1) { |
|
40 |
date = parts[0]; |
|
41 |
parts = date.match(/../g); |
|
42 |
if (date.length == 8) { |
|
43 |
parts[ns._date_format.y] += parts.splice(ns._date_format.y + 1, 1) |
|
44 |
} |
|
45 |
else |
|
46 |
if (date.length == 6 || date.length == 4) { |
|
47 |
} |
|
48 |
else |
|
49 |
if (date.length == 1 || date.length == 2) { |
|
50 |
parts = [] |
|
51 |
parts[ ns._date_format.y ] = today.getFullYear(); |
|
52 |
parts[ ns._date_format.m ] = today.getMonth() + 1; |
|
53 |
parts[ ns._date_format.d ] = date; |
|
54 |
} |
|
55 |
else { |
|
56 |
return undefined; |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
if (parts.length == 3) { |
|
61 |
var year = +parts[ ns._date_format.y ] || 0 * 1 || (new Date()).getFullYear(); |
|
62 |
if (year < 100) { |
|
63 |
year += year > 70 ? 1900 : 2000; |
|
64 |
} |
|
65 |
date = new Date( |
|
66 |
year, |
|
67 |
(parts[ ns._date_format.m ] || 0) * 1 - 1, // Months are 0-based. |
|
68 |
(parts[ ns._date_format.d ] || 0) * 1 |
|
69 |
); |
|
70 |
} else if (parts.length == 2) { |
|
71 |
date = new Date( |
|
72 |
(new Date()).getFullYear(), |
|
73 |
(parts[ (ns._date_format.m > ns._date_format.d) * 1 ] || 0) * 1 - 1, // Months are 0-based. |
|
74 |
(parts[ (ns._date_format.d > ns._date_format.m) * 1 ] || 0) * 1 |
|
75 |
); |
|
76 |
} else |
|
77 |
return undefined; |
|
40 | 78 |
|
41 | 79 |
return isNaN(date.getTime()) ? undefined : date; |
42 | 80 |
}; |
... | ... | |
59 | 97 |
if (ns._number_format.decimalSep == ',') |
60 | 98 |
amount = amount.replace(/\./g, "").replace(/,/g, "."); |
61 | 99 |
|
62 |
amount = amount.replace(/[\',]/g, "") |
|
100 |
amount = amount.replace(/[\',]/g, "");
|
|
63 | 101 |
|
64 | 102 |
// Make sure no code wich is not a math expression ends up in eval(). |
65 | 103 |
if (!amount.match(/^[0-9 ()\-+*/.]*$/)) |
66 | 104 |
return 0; |
67 | 105 |
|
106 |
amount = amount.replace(/^0+/, ''); |
|
107 |
|
|
68 | 108 |
/* jshint -W061 */ |
69 | 109 |
try { |
70 | 110 |
return eval(amount); |
js/t/kivi/parse_format_date.js | ||
---|---|---|
12 | 12 |
assert.deepEqual(kivi.parse_date("25.12."), new Date((new Date).getFullYear(), 11, 25)); |
13 | 13 |
assert.deepEqual(kivi.parse_date("25.12"), new Date((new Date).getFullYear(), 11, 25)); |
14 | 14 |
|
15 |
assert.deepEqual(kivi.parse_date("2512"), new Date((new Date).getFullYear(), 11, 25)); |
|
16 |
assert.deepEqual(kivi.parse_date("25122015"), new Date(2015, 11, 25)); |
|
17 |
assert.deepEqual(kivi.parse_date("251215"), new Date(2015, 11, 25)); |
|
18 |
assert.deepEqual(kivi.parse_date("25"), new Date((new Date).getFullYear(), (new Date).getMonth(), 25)); |
|
19 |
assert.deepEqual(kivi.parse_date("1"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); |
|
20 |
assert.deepEqual(kivi.parse_date("01"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); |
|
21 |
|
|
15 | 22 |
assert.deepEqual(kivi.parse_date("Totally Invalid!"), undefined); |
23 |
assert.deepEqual(kivi.parse_date(":"), undefined); |
|
24 |
assert.deepEqual(kivi.parse_date("::"), undefined); |
|
25 |
assert.deepEqual(kivi.parse_date(""), undefined); |
|
16 | 26 |
}); |
17 | 27 |
|
18 | 28 |
QUnit.test("kivi.parse_date function for German date style with slashes", function( assert ) { |
... | ... | |
100 | 110 |
assert.deepEqual(kivi.format_date(new Date(2008, 1, 29)), "2008-02-29"); |
101 | 111 |
assert.deepEqual(kivi.format_date(new Date(2014, 11, 11)), "2014-12-11"); |
102 | 112 |
|
113 |
assert.deepEqual(kivi.parse_date("1225"), new Date((new Date).getFullYear(), 11, 25)); |
|
114 |
assert.deepEqual(kivi.parse_date("20151225"), new Date(2015, 11, 25)); |
|
115 |
assert.deepEqual(kivi.parse_date("151225"), new Date(2015, 11, 25)); |
|
116 |
assert.deepEqual(kivi.parse_date("25"), new Date((new Date).getFullYear(), (new Date).getMonth(), 25)); |
|
117 |
assert.deepEqual(kivi.parse_date("1"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); |
|
118 |
assert.deepEqual(kivi.parse_date("01"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); |
|
119 |
|
|
103 | 120 |
assert.deepEqual(kivi.format_date(new Date(undefined, undefined, undefined)), undefined); |
104 | 121 |
}); |
Auch abrufbar als: Unified diff
kivi.js: parse_amount und parse_date quirks
- Parsing von 1,2,4,6,8-stelligen Daten ohne Separator
- Parsing von yyyy-mm-dd Format ohne Separator
- Parsing von 0-Präfixen bei Zahlen