# Date Format, Null or empty values

**URL:** <https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [September 9, 2023, 1:19am UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936 "2023-09-09T01:19:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 9, 2023, 1:19am UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/1 "2023-09-09T01:19:54Z")

</div>

Hi,  
I want to display an empty box if no date is available, but I want to format it when the date is available.  
If I use: `if (isnull({value}), '', {value})` with no formatting, I get an empty box, but if I use this: `if(isnull({value}), '', dateformat({value}, 'YYYY MM d'))` then I get `1970 12 31`.

I tried using empty quotes in the expression as well, same results. Not sure what to try next.

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 9, 2023, 1:29am UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/2 "2023-09-09T01:29:07Z")

</div>

"Empty box" isn't a Perspective component. Do you mean blank "label" or some other component?

1. Where is `{value}` coming from? Is it a timestamp?
2. What does the Edit Binding dialog Preview (bottom left corner) show is the incoming value?

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 12, 2023, 1:24am UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/3 "2023-09-12T01:24:09Z")

</div>

Well, by "empty box" I mean both a blank label and blank text field. `{value}` is coming from a `date` field in SQL Server.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/f/bfb3cf3189fb4e24d8f6a139a9fcd1e93257beee.png)

This is the expression binding on the object to which it is bound:  
`try({view.custom.arr_Item_Req_Data}[0, 'createdAt'], '')`

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 12, 2023, 6:10am UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/4 "2023-09-12T06:10:45Z")

</div>

It looks like you have an empty string coming from the database - not a `null`. (See the incoming "Property" value at the bottom is blank rather than "null".) That suggests that either your database column's type is not what you think or the query is returning a string rather than a date.

You can debug by commenting out your expression (`//` in front of expression) and replace it with the [typeOf](https://docs.inductiveautomation.com/display/DOC81/typeOf) function and see what the binding preview Expression reveals.

If you can't fix the database then try checking for an empty string,  
`if({value} = '', '', dateformat({value}, 'YYYY MM d'))`

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 12, 2023, 8:18pm UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/5 "2023-09-12T20:18:53Z")

</div>

Aha! I like that `typeOf` function. I determined that the only time I am getting an empty string is when I want to add a new record to the DB, and not reading data from the DB...

So, with that in mind, the next step is to write an expression that can handle an empty string - which will leave the `props.text` empty, and a `NULL` value - which will also leave the `props.text` empty. Would a `try...catch` work or some sort of `switch`/`case` statement? Not sure if either of those are valid in the expression language. Well, I see that `case` can be used, BUT:

```auto
case(
	{value},
	NULL, '',
	'', '')

```

This doesn't work.

So, the solution becomes:

```auto
if(isnull({value}), '', 
	if({value} = '', '', dateformat({value}, 'YYYY MM d')))

```

Super easy, barely an inconvenience! Thank you, my electronic friend!

---

<div class="post-metadata">

**Author:** ![mdemaris](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mdemaris/32/54639_2.png) [@mdemaris](https://forum.inductiveautomation.com/u/mdemaris)\
**Post date:** [September 12, 2023, 8:23pm UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/6 "2023-09-12T20:23:04Z")

</div>

Follow up:  
In a text box, where I want a user to enter a date, how can I format that as something like `YYYY MMM d` and still allow it to be bidirectional? As soon as I add any transform, I no longer have the bidirectional option.

Update: I added the expression transform above to the `custom.prop` to which the component is bound, and the date is displayed correctly...for now. Next step: test the user input.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 12, 2023, 9:49pm UTC](https://forum.inductiveautomation.com/t/date-format-null-or-empty-values/78936/7 "2023-09-12T21:49:16Z")

</div>

> [@mdemaris](#):
>
> ... How can I format that as something like `YYYY MMM d` and still allow it to be bidirectional?

Use a datepicker component. There are too many ways a user can mess it up otherwise. Bi-directional binding is then trivially simple.
