Before we start to talk about time or dates, we need to understand how computers, as well as our Platform, perceive time. The answer to this is simple: they perceive time as a long number which looks something like this:
1254412213396
This number represents a complete date. In this case, the exact time and date represented by this number would look like this:
2009-10-01T17:50:13.396+0200
If that number generates a valid date, what date would be generated if the number is zero?
1970-01-01T01:00:00.000+0100
Time calculation for computers started at midnight on January 1, 1970. The milliseconds have been ticking ever since.
To get Time and Date in the Platform, use one of the following functions:
NOW() – Returns the exact date and time.TODAY() – Returns a date starting at exactly 00:00:00 hour in the morning.PARSEDATE('2007-06-24T00:00:00.000+0200') – Returns a date. The date must conform or must be parsable according to one of the Format Patterns defined in Studio.
As discussed, time within a computer is a single long variable displaying milliseconds since January 1, 1970. This sounds simple, however it is not very human-readable. Through the use of various functions, our Platform allows calculating and converting dates to human readable and internationalized date patterns.
The expression:
DATEFORMAT(NOW(), 'dd.MM.yyyy', 'de_CH')
will output today's date in the format dd.MM.yyyy, e.g. 27.08.2019.
You can design your own format patterns for dates and times using the provided list of units:
These units can be used with the DATEFORMAT and DATEPARSE functions, as well as with the Format Pattern Business Object.
| Unit | Explanation | Type | Example |
|---|
| G | Era designator | TEXT | AD |
| y | Year | NUMBER | 1998; 98 |
| Y | Week Year | NUMBER | 2012; 12 |
| M | Month in the year | TEXT & NUMBER | 08; Aug; August |
| d | Day in month | NUMBER | 10 |
| h | Hour in am/pm (1–12) | NUMBER | 12 |
| H | Hours in day (0–23) | NUMBER | 0 |
| m | Minutes in hour | NUMBER | 30 |
| s | Seconds in minute | NUMBER | 55 |
| S | Millisecond | NUMBER | 978 |
| E | Day in week | TEXT | Tuesday |
| u | Day number in week (1 = Monday, 2 = Tuesday etc.) | NUMBER | 2 |
| D | Day in year | NUMBER | 255 |
| F | Day of week in month | NUMBER | 2 |
| w | Week in year | NUMBER | 27 |
| W | Week in month | NUMBER | 2 |
| a | am/pm marker | TEXT | PM |
| k | Hour in day (1–24) | NUMBER | 24 |
| K | Hours in am/pm (0–11) | NUMBER | 0 |
| z | Time zone (General) | TEXT | CEST; Central European Summer Time |
| Z | Time zone (RFC 822) | NUMBER | +0200 |
| X | Time zone (ISO 8061) | NUMBER | +02; +0200; +02:00 |
| ' | Escape for text | DELIMITER | (none) |
| ' | Single quote | LITERAL | ' |
Characters that are not letters are treated as quoted text. That is, they appear in the formatted text even if they are not enclosed within single quotes.
The number of unit specifiers you enter also determines the format. For example, the zz pattern results in "PDT," but the zzzz pattern generates "Pacific Daylight Time." The following table summarizes these rules:
| Type | Number of Unit Specifiers | Result |
|---|
| TEXT | 1–3 | Abbreviated form, if one exists |
| TEXT | >=4 | Full form (Complete Text) |
| NUMBER | Minimum number of digits is required | Shorter numbers are padded with zeros (for a year, if the count of 'y' is 2, then the year is truncated to 2 digits) |
| TEXT&NUMBER | 1–2 | Number form |
| TEXT&NUMBER | 3 | Text form (abbreviation) |
| TEXT&NUMBER | >=4 | Text form (complete text) |
The available units and date patterns are based on the implementation in Java, see the
Java documentation for more details.
Maybe you don't want to know, but if so, there is an easy way to find out:
DAYSSINCE(PARSEDATE('1967-05-12'))
Calculating with times and dates is not too complicated. The most simple example is to deal with the milliseconds:
day in milliseconds = 1000*60*60*24
To get the exact current time in milliseconds, use the following function:
TIMESTAMP()
The age of a person (in years) can be calculated with the following command:
TIMEDIFF(PARSEDATE('1967-05-12'), NOW(), 'y')
In certain cases it is necessary to add a certain amount of time to known date. For this, the Platform provides the function TIMEADD:
TIMEADD(NOW(), '1D');
TIMEADD(PARSEDATE('1967-05-12'), '42Y');
TIMEADD(NOW(), '1M 1m 30s');
TIMEADD(NOW(), '-1w');
To compare two dates, "A" and "B", use the function DATECOMPARE. The function returns:
- "–1" if A is before B
- 0 if A and B are at the same time
- "1" if A is after B
Two further optional parameters are available allowing you to:
- Specify if an undefined date (
null) is by default always later or earlier than any other date. If set to true, the null value will always be after any date. - Define the precision of the comparison. Use the following abbreviations for precision:
- y for year
- M for month
- W/w for week
- D/d for day
- H/h for hour
- m for minute
- s for second
- S for millisecond
DATECOMPARE(TODAY(), PARSEDATE('1967-05-12'));
DATECOMPARE(PARSEDATE('2019-10-12'), PARSEDATE(''), true);
DATECOMPARE(TODAY(), PARSEDATE('2019-10-12'), true, y);
The result of rounding a date up or down is a date which points to the beginning or end of a year, month, week, day, or even the beginning of a minute or second.
The TIMEROUND function rounds by a natural decision. Here, we are rounding on the hour and more than half of the hour has passed, so the function rounds up:
TIMEROUND(PARSEDATE('2006-03-02T01:31:00.000+0100'), 'H')
Returns: 2006-03-02T02:00:00.000+0100
The additional parameter up or down can be used to force the rounding direction:
TIMEROUND(PARSEDATE('2006-03-02T01:31:00.000+0100'), 'H', 'up')
Returns: 2006-03-02T02:00:00.000+0100
TIMEROUND(PARSEDATE('2006-03-02T01:31:00.000+0100'), 'H', 'down')
Returns: 2006-03-02T01:00:00.000+0100
To calculate and make decisions based on dates, it is sometimes useful to extract a single value or field from a date. For example, extract the year, month, day, day in week, or current hour.
DATEFIELD(PARSEDATE('2006-01-01'), 'y')
Returns: 2006
DATEFIELD(PARSEDATE('2006-01-01T13:27:21.000+0100'), 'H')
Returns: 13
The following functions can be used to parse date Strings in the Platform:
DATEPARSE – Is the counterpart of DATEFORMAT. Both functions require a pattern to be specified as a parameter and do not rely on pre-defined Format Pattern Business Objects. For example:
DATEPARSE('14.08.1980', 'dd.MM.yy')
Returns: 1980-08-14T00:00:00.000+0100
TODATE and PARSEDATE – Both functions use 'Date Input' Format Pattern Business Objects for parsing date Strings. This means you can add, change, or remove supported date patterns in your Solution.
Examples (based on the 'Date Input' Format Pattern Business Objects available by default in the Platform after installation):
TODATE('6/24/07')
Returns: 2007-06-24T00:00:00.000+0200
PARSEDATE('22.11.2011')
Returns: 2011-11-22T00:00:00.000+0100
What are the differences between TODATE and PARSEDATE?
- If the date String cannot be parsed,
TODATE throws an exception, while PARSEDATE returns null. TODATE can also convert a millisecond timestamp (Number) to a date (typically a scenario encountered during integration of third-party systems or web service integration):
TODATE(1254412213396)
Returns: 2009-10-01T17:50:13.396+0200
PARSEDATE supports specifying a time zone as an optional parameter.
Making decisions based on Dates and Time is an arithmetic task. Let's look at a few common examples.
To determine if a person is older than 18, the following simple expression returns true or false:
NOW() > TIMEADD(PARSEDATE('1967-05-12'), '18y')
To determine if a given date is within a certain time range, a combination of functions returns true or false:
TIMESTAMP(PARSEDATE('2009-05-12')) < TIMESTAMP() and TIMESTAMP() < TIMESTAMP(PARSEDATE('2010-06-12'))