Using Math with Date Fields
In the last post, we added some math formulas to numeric fields, but pointed out that this does not work with traditional Date fields. So what are we doing here? There is a different method that you can use that will allow you to do this.
Instead of using a Date field, we are going to use a DIESEL expression. If that sounds strange, don't fear. I will not cover intricate details regarding DIESEL functions and you will not need it for this tutorial.
Let's say we want to create a field that always shows us today plus 7 days. In other words, one week from today in the future. Of course the beauty of it being a field, is that it will always show us 7 days in the future, even if we open this drawing up 6 months from now.
- Start by creating a FIELD. Under Field category, choose "Other", then "Diesel Expression".
- All you get is a blank slate on which to enter your Diesel code. Good luck, right? Fortunately, the Diesel functions are documented fairly well in the customization guide, and we will use an example found there.
- Copy the following and paste it into the dialog: $(edtime, $(getvar,date),DDD"," DD MON YYYY - H:MMam/pm)
- Ok, that is straight out of the customization guide. Press OK and place the field. You should get the current date and time. So how do we add a week?
- Get back into the Field Editor and let's edit the Diesel Expression.
- Change the code so that is looks like this (I have highlighted the changes in bold red): $(edtime, $(+, 7, $(getvar,date)),DDD"," DD MON YYYY - H:MMam/pm)
- Now click OK to update the field. You should get a date that is one week in the future. Both of mine are shown here.
What we did was add a "+" function, surrounding the $(getvar,date) function. The $(getvar, date) piece of the code returns a value where each whole number represents one day. So adding 7 to this adds 7 days. The $ is there to start a new function, the "+" is the function, and the commas separate the arguments. We need a closing parenthesis on the other end to balance things out.
The rest of that Diesel code you see is there to format the string into a readable date. You can remove, change the order of, or change the display of any of the components you see (year, month, day, hour, minute, etc.). Again, details on this are in the Customization Guide. I'll plan on covering this in more detail, in the next post.