spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Using DateRegExBuilder for 'MM/DD/YYYY'
author
Ben Vogt <[email protected]>
date
2017-03-26 22:54:37
stats
3 file(s) changed, 27 insertions(+), 10 deletions(-)
files
README.md
src/RawFormulas/Date.ts
src/RawFormulas/Utils.ts
 1diff --git a/README.md b/README.md
 2index 137bebe..27aaf10 100644
 3--- a/README.md
 4+++ b/README.md
 5@@ -57,6 +57,10 @@ YYYY/MM/DD       YEAR_MONTHDIG_DAY_SLASH_DELIMIT
 6 YYYY-MM-DD       YEAR_MONTHDIG_DAY_HYPHEN_DELIMIT
 7 YYYY.MM.DD       YEAR_MONTHDIG_DAY_DOT_DELIMIT
 8 YYYY MM DD       YEAR_MONTHDIG_DAY_SPACE_DELIMIT
 9+MM/DD/YYYY       MONTHDIG_DAY_YEAR_SLASH_DELIMIT
10+MM-DD-YYYY       MONTHDIG_DAY_YEAR_HYPHEN_DELIMIT
11+MM.DD.YYYY       MONTHDIG_DAY_YEAR_DOT_DELIMIT
12+MM DD YYYY       MONTHDIG_DAY_YEAR_SPACE_DELIMIT
13 YYYY, MM, DD     YEAR_MONTHDIG_DAY_COMMA_DELIMIT
14 Month DD YYYY    MONTHNAME_DAY_YEAR_COMMON_DELIMITERS
15 DD Month YYYY    DAY_MONTHNAME_YEAR_COMMON_DELIMITERS
16diff --git a/src/RawFormulas/Date.ts b/src/RawFormulas/Date.ts
17index 8f4fa71..dd4c79e 100644
18--- a/src/RawFormulas/Date.ts
19+++ b/src/RawFormulas/Date.ts
20@@ -136,17 +136,12 @@ var DATEVALUE = function (...values) : number {
21   var dateString = TypeCaster.firstValueAsString(values[0]);
22   var m;
23 
24-  // Check YYYY/MM/DD
25+  // Check YEAR_MONTHDIG_DAY_SLASH_DELIMIT, YYYY/MM/DD, 1992/06/24
26   if (m === undefined) {
27     // For reference: https://regex101.com/r/uusfi7/5
28-    var REG = /^\s*(([0-9][0-9][0-9][0-9])|([1-9][0-9][0-9]))\/([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\s*$/;
29-    REG = DateRegExBuilder.DateRegExBuilder()
30+    const REG = DateRegExBuilder.DateRegExBuilder()
31       .start()
32-      .YYYY()
33-      .SLASH_DELIMITOR()
34-      .MM()
35-      .SLASH_DELIMITOR()
36-      .DD()
37+      .YYYY().SLASH_DELIMITOR().MM().SLASH_DELIMITOR().DD()
38       .end()
39       .build();
40     var matches = dateString.match(REG);
41@@ -170,10 +165,15 @@ var DATEVALUE = function (...values) : number {
42     }
43   }
44 
45-  // Check MM/DD/YYYY
46+  // Check MONTHDIG_DAY_YEAR_SLASH_DELIMIT, MM/DD/YY(YY), 06/24/1992 or 06/24/92
47   if (m === undefined) {
48     // For reference: https://regex101.com/r/yHraci/5
49-    var matches = dateString.match(/^\s*([1-9]|0[1-9]|1[0-2])\/([1-9]|[0-2][0-9]|3[0-1])\/(([0-9][0-9][0-9][0-9])|([1-9][0-9][0-9])|[0-9]{0,3})\s*$/);
50+    const REG = DateRegExBuilder.DateRegExBuilder()
51+      .start()
52+      .MM().SLASH_DELIMITOR().DD().SLASH_DELIMITOR().YY_OP_YY()
53+      .end()
54+      .build();
55+    var matches = dateString.match(REG);
56     if (matches && matches.length === 6) {
57       var years = parseInt(matches[3]);
58       var months = parseInt(matches[1]) - 1; // Months are zero indexed.
59diff --git a/src/RawFormulas/Utils.ts b/src/RawFormulas/Utils.ts
60index d0fdd86..62a3500 100644
61--- a/src/RawFormulas/Utils.ts
62+++ b/src/RawFormulas/Utils.ts
63@@ -441,6 +441,16 @@ class DateRegExBuilder {
64     return this;
65   }
66 
67+  /**
68+   *
69+   * @returns {DateRegExBuilder}
70+   * @constructor
71+   */
72+  YY_OP_YY() : DateRegExBuilder {
73+    this.regexString += "(([0-9][0-9][0-9][0-9])|([1-9][0-9][0-9])|[0-9]{0,3})";
74+    return this;
75+  }
76+
77   /**
78    * Builds the regular expression.
79    * @returns {RegExp} Built up string as a regular expression.