• Articles

Date Comparisons and Calculations Using Math Symbols and Durations

Taken from the tip of the week for October 5, 2007 this article explains how the [Date] and [Duration] data types in Lasso can be used to perform date comparisons and date calculations using the familiar built-in symbols for math and string operations.

Introduction

Lasso includes a built-in [Date] data type which represents a particular date/time and a [Duration] data type which represents the difference between two date/times. Together, the [Date] and [Duration] types provide support for performing date comparisons and date calculations using the built-in symbols including less than <, greater than >, equals ==, as well as addition +, and subtraction -.

This tip shows how the [Date] and [Duration] data types can be used as objects in order to make performing date calculations as natural as performing math or string operations.

Creating a Date

The current date/time can be returned using the [Date] tag without any parameters. The result of the [Date] tag is actually a date object which can be stored in a variable.

When the value of this tag is output it returns the current date/time using the current formatting. By default, the U.S. date/time format shown here is output. The same output can be generated by simply placing [Date] on a Lasso page.

``  [Var: 'myDate' = (Date)]``
``  [\$myDate]  -> 10/05/2007 09:00:00``

The [Date] tag can also be used to convert a string value into a date. The [Date] tag automatically recognizes several formats including the U.S. date/time format shown above, MySQL date/time format "2007-10-05 08:57:25", and several formats which are encountered in Internet headers. The time is optional in most formats. No -Format parameters is necessary when using these date formats.

``````  [Date: '10/05/2007 09:00:00']
[Date: '2007-10-05 09:00:00']
[Date: '20071005090000']
[Date: 'Fri Oct  5 09:00:00 2007']``````

Other formats require a -Format parameter which tells Lasso what format the date has been specified in. A common European format is -Format='%d/%m/%Y %T' and ISO format is -Format='%QT%T'. These could be used as follows.

``````  [Date: '2007-10-05 09:00:00', -Format='%QT%T]
[Date: '5/10/2007 09:00:00', -Format='%d/%m/%Y %T]``````

A list of date format tokens can be found in the online Lasso Reference:

Date Comparisons

Now that we have a date object we can compare it to other date values to determine if a date is in the future, in the past, or between two dates. The symbols which are used for numeric comparisons are used to compare dates.

The following code determines if the current date is between the first and last day of 2007. Lasso automatically converts the right-hand side of each comparison to the same type as the left-hand side. As long as the right-hand sides are a valid default date format they can simply be specified as string. Note that the [Date] object should always be on the left-hand side.

``````  [If: (Date >= '2007-01-01') && (Date <= '2007-12-31')]
It is 2007!
[/If]``````

Two dates can be compared similarly. The following code checks that the date \$myDate is between the start and end of Spring in the northern hemisphere, 2007.

``````  [Var: 'springStart' = (Date: '2007-03-21')]
[Var: 'springEnd' = (Date: '2007-06-21')]
[Var: 'myDate' = (Date: '2007-4-12')]
[If: (\$myDate >= \$springStart) && (\$myDate <= \$springEnd)]
The date is in Spring 2007!
[/If]``````

The member tags of the [Date] data type make many other types of comparisons possible. For example, the year comparison above can be more easily checked using the [Date->Year] tag to check just the year from the target date.

``````  [If: (Date->Year == 2007)]
It is 2007!
[/If]``````

The same comparison can be made using a date object stored in a variable.

``````  [Var: 'mydate' = (Date: '10/05/2007 09:00:00')]
[If: (\$myDate >= '2007-01-01') && (\$myDate <= '2007-12-31')]
The date is in 2007!
[/If]``````

Date Calculations

The [Data] and [Duration] data types support direct manipulation using the math symbols + and -. The [Duration] data type is necessary since it really doesn't make sense to add two dates together. A duration can be added or subtracted to a date and a duration is the result of subtracting one date from another. Durations can also be added and subtracted.

The date seven days in the future can be found by adding a [Duration] of seven days to the current date.

``  [Date + (Duration: -Day=7)]  -> 10/12/2007 09:00:00``

Durations can be specified in one of two formats. They can be initialized using parameters -Week, -Day, -Hour, -Minute, and -Second. Or, they can be initialized using a time format like [Duration: '1:0:0'] for one hour. The number of hours specified can be greater than 24.

The time 12 hours ago can be calculated using any of these expressions. The last expressions uses Lasso's automatic casting to convert the string into a duration

Note that it is also possible to use the tags [Date->Add] and [Date->Subtract] to perform date calculations.

The duration between two dates can be found by subtracting one date from another. The following expression determines how much time has elapsed since the start of the year. The resulting duration is cast to a string showing that we are 6,656 hours into 2007.

``  [Date - '2007-1-1'] -> 6656:00:00``
``````  [Date - (Duration: -Hour=12)] -> 10/4/2007 21:00:00
[Date - (Duration: '12:00:00')]  -> 10/4/2007 21:00:00
[Date - '12:00:00']  10/4/2007 -> 21:00:00``````

Since the result of this expression is actually a duration it is possible to use the member tags of the duration to display the results in different ways. In particular, we might want to know how many days that represents. We could divide 6,656 by 24 or we can use the [Duration->Day] tag as follows. Note that the actual number of days represented as a decimal is 277.333. Lasso rounds the result to generate an integer.

``  [(Date - '2007-1-1')->Day]  277``

Date Comparisons and Calculations

If we combine the techniques above then we can do some interesting date comparisons. For example, we can check if \$myDate is within the next week by adding a duration of 7 days to the current date.

`````` [Var: 'myDate' = (Date: '2007-10-10')]
[If: (\$myDate >= Date) && (\$myDate < Date + (Duration: -Day=7))]
The date is in the next week!
[/If]``````

Or, we can find out if Project A took longer than Project B by comparing the differences between the start and end dates for each project.

``````  [Var: 'projectAStart' = (Date: '2006-08-14')]
[Var: 'projectAEnd' = (Date: '2006-09-15')]
[Var: 'projectBStart' = (Date: '2007-03-09')]
[Var: 'projectBEnd' = (Date: '2007-04-06')]``````
``````  [If: (\$projectAEnd - \$projectAStart) < (\$projectBEnd - \$projectBStart)]
Project A took less time than Project B.
[Else]
Project B took less time than Project A.
[/If]``````

More information about all of the tags used in this tip of the week can be found in the Lasso 8.5 Language Guide or in the online Lasso Reference

Author: Fletcher Sandbeck
Created: 5 Oct 2007