# Node property validation function for date formatting

**URL:** <https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457>\
**Category:** Developing Nodes\
**Created:** [16 January 2020 14:27 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457 "2020-01-16T14:27:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnwalicki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnwalicki/32/8848_2.png) [@johnwalicki](https://discourse.nodered.org/u/johnwalicki)\
**Post date:** [16 January 2020 14:27 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/1 "2020-01-16T14:27:17Z")

</div>

I am creating a new Node-RED node that requires a date using a yyyymmdd format. Has anyone created a node that implements a property validation function on date formatting? I want to filter out or rewrite "yyyy-mm-dd" or "yyyy/mm/dd" as yyyymmdd

All I have now is:

```auto
    defaults: {
        date: { value:0, validate:RED.validators.number(), required:true},
    },

```

I give a hint in the input

```auto
  <div class="form-row">
   <label for="node-input-date"><i class="fa fa-calendar"></i> Select Date</label>
    <input type="text" id="node-input-date" placeholder="yyyymmdd">
  </div>

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 January 2020 14:40 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/2 "2020-01-16T14:40:55Z")

</div>

Hi @johnwalicki

the validator can only be used to check if it is valid - it can't do any rewriting. You'd have to implement that part yourself.

I'd use a regex validator for this - as you need to ensure its a certain length. Something like:

```auto
validate:RED.validators.regex(/^\d{8}/)

```

That would do a crude check for exactly 8 digits. If you wanted to check those 8 digits represent a valid year, then you'd need a custom validator...

```auto
validate: function(v) {
   if (!/^\d{8}/.test(v)) { return false; }
   var year = v.substring(0,4);
   var month = v.substring(4,6);
   var day = v.substring(6,8);
   // Date.parse will return NaN if this isn't a valid date
   var time = Date.parse(year+"-"+month+"-"+day);
   return !isNaN(time);
}

```

---

<div class="post-metadata">

**Author:** ![johnwalicki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnwalicki/32/8848_2.png) [@johnwalicki](https://discourse.nodered.org/u/johnwalicki)\
**Post date:** [16 January 2020 14:54 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/3 "2020-01-16T14:54:35Z")

</div>

Thanks Nick - Your validate function snippet is very useful and makes the node more robust in handling bad input. I've incorporated it into my new node-red-contrib node.

```auto
defaults: {
        date: { value:0, required:true, validate: function(v) {
             if (!/^\d{8}/.test(v)) { return false; }
             var year = v.substring(0,4);
             var month = v.substring(4,6);
             var day = v.substring(6,8);
             // Date.parse will return NaN if this isn't a valid date
             var time = Date.parse(year+"-"+month+"-"+day);
             return !isNaN(time);
          }
        }
 }

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [16 January 2020 16:37 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/4 "2020-01-16T16:37:30Z")

</div>

If you wanted to be kinder to your users, it would also be easy to implement a re-write function upon user input.

The Editor gets access to jQuery and you can attach a function of the on-blur or on-change events, check the format on the fly and re-write the input.

---

<div class="post-metadata">

**Author:** ![johnwalicki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnwalicki/32/8848_2.png) [@johnwalicki](https://discourse.nodered.org/u/johnwalicki)\
**Post date:** [16 January 2020 17:20 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/5 "2020-01-16T17:20:37Z")

</div>

Great idea - will add this to the todo list. Want to publish the first iteration of the node-red-contrib-twc-weather then circle back to enhancements.

---

<div class="post-metadata">

**Author:** ![johnwalicki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnwalicki/32/8848_2.png) [@johnwalicki](https://discourse.nodered.org/u/johnwalicki)\
**Post date:** [21 January 2020 22:12 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/6 "2020-01-21T22:12:15Z")

</div>

I included this enhancement in the recently released node-red-contrib-twc-weather nodes. The node will accept yyyymmdd, yyyy/mm/dd, yyyy-mm-dd or a timestamp. That allows you to use the Node-RED Dashboard date picker to select a date in the past and get the weather history.

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [24 February 2020 22:04 UTC](https://discourse.nodered.org/t/node-property-validation-function-for-date-formatting/20457/7 "2020-02-24T22:04:47Z")

</div>

The [node-red-contrib-sun-position](https://flows.nodered.org/node/node-red-contrib-sun-position) node has a `time compare and change` node which is designed for time formatting.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cffcdf9f67268a39411cf3a49055df7dd1ac8571.png)

or

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/208214dfbac5ce7e83d5a41ef5191b1b568c4347.png)
