commit
message
Checking for '2012-6-6' style date formats in DATEVAUE
author
Ben Vogt <[email protected]>
date
2017-02-26 20:56:14
stats
3 file(s) changed,
30 insertions(+),
2 deletions(-)
files
src/RawFormulas/Date.ts
src/RawFormulas/Utils.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
2index 82e8da4..6369a7e 100644
3--- a/src/RawFormulas/Date.ts
4+++ b/src/RawFormulas/Date.ts
5@@ -51,7 +51,9 @@ var DATEVALUE = function (...values) : number {
6 var dateNumber;
7 if (RegExUtil.matchDateStringYearMonthDaySlash(dateString)) { // Check "2012/1/23"
8 dateNumber = new ExcelDate(moment(dateString, "Y/M/D")).toNumber();
9- } if (RegExUtil.matchDateStringMonthDayYearSlash(dateString)) { // Check "1/23/2012"
10+ } else if (RegExUtil.matchDateStringYearMonthDayHyphen(dateString)) { // Check "2012-1-23"
11+ dateNumber = new ExcelDate(moment(dateString, "Y-M-D")).toNumber();
12+ } else if (RegExUtil.matchDateStringMonthDayYearSlash(dateString)) { // Check "1/23/2012"
13 dateNumber = new ExcelDate(moment(dateString, "M/D/Y")).toNumber();
14 }
15 if (dateNumber === undefined) {
16diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
17index 40d8f24..b98d7eb 100644
18--- a/src/RawFormulas/Utils.ts
19+++ b/src/RawFormulas/Utils.ts
20@@ -370,6 +370,7 @@ class RegExUtil {
21 private static MONTH_DAY_YEAR_SLASH_REGEX = /^\s*([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\/([1-9][0-9][0-9][0-9])\s*$/;
22 private static DAY_MONTH_YEAR_SLASH_REGEX = /^\s*([1-9]|[0-2][0-9]|3[0-1])\/([1-9]|0[1-9]|1[0-2])\/([1-9][0-9][0-9][0-9])\s*$/;
23 private static YEAR_MONTH_DAY_SLASH_REGEX = /^\s*([1-9][0-9][0-9][0-9])\/([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\s*$/;
24+ private static YEAR_MONTH_DAY_HYPHEN_REGEX = /^\s*([1-9][0-9][0-9][0-9])-([1-9]|0[1-9]|1[0-2])-([1-9]|[0-2][0-9]|3[0-1])\s*$/;
25
26 /**
27 * Match dates in the form "mm/dd/yyyy" or "m/d/yyyy".
28@@ -390,7 +391,7 @@ class RegExUtil {
29 }
30
31 /**
32- * Match dates in the form "yyyy/mm/dd" or "yyyy/mm/dd".
33+ * Match dates in the form "yyyy/mm/dd" or "yyyy/m/d".
34 * @param dateString to match
35 * @returns {RegExpMatchArray} matches or null
36 */
37@@ -398,6 +399,15 @@ class RegExUtil {
38 return dateString.match(this.YEAR_MONTH_DAY_SLASH_REGEX);
39 }
40
41+ /**
42+ * Match dates in the form "yyyy-mm-dd" or "yyyy-m-d".
43+ * @param dateString to match
44+ * @returns {RegExpMatchArray} matches or null
45+ */
46+ static matchDateStringYearMonthDayHyphen(dateString : string) : RegExpMatchArray | null {
47+ return dateString.match(this.YEAR_MONTH_DAY_HYPHEN_REGEX);
48+ }
49+
50 }
51
52 export {
53diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
54index e263af0..a0586a8 100644
55--- a/tests/FormulasTest.ts
56+++ b/tests/FormulasTest.ts
57@@ -796,7 +796,22 @@ assertEquals(DATEVALUE("1992/1/24"), 33627);
58 assertEquals(DATEVALUE("1992/12/21"), 33959);
59 assertEquals(DATEVALUE("1992/01/31"), 33634);
60 assertEquals(DATEVALUE("1992/1/13"), 33616);
61-
62+// yyyy-m-d
63+assertEquals(DATEVALUE("1992-6-24"), 33779);
64+assertEquals(DATEVALUE("1992-06-24"), 33779);
65+assertEquals(DATEVALUE("1999-1-01"), 36161);
66+assertEquals(DATEVALUE("2222-1-01"), 117610);
67+assertEquals(DATEVALUE("1902-9-02"), 976);
68+assertEquals(DATEVALUE("1902-9-2"), 976);
69+assertEquals(DATEVALUE("4243-11-3"), 856071);
70+assertEquals(DATEVALUE(" 1992-04-19 "), 33713);
71+assertEquals(DATEVALUE("1992-5-20"), 33744);
72+assertEquals(DATEVALUE("1992-6-21"), 33776);
73+assertEquals(DATEVALUE("1992-9-29"), 33876);
74+assertEquals(DATEVALUE("1992-1-24"), 33627);
75+assertEquals(DATEVALUE("1992-12-21"), 33959);
76+assertEquals(DATEVALUE("1992-01-31"), 33634);
77+assertEquals(DATEVALUE("1992-1-13"), 33616);
78
79
80