---
schema_version: "1"
id: a79b683ccdbfe853d70383408afa0bc83ab5120243b50d6879630697fa7165ce
title: Date Functions for Formulas
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/analytics/264-0-0/bi_integrate_recipe_datefunctions.md
canonical_url: https://help.salesforce.com/s/articleView?id=analytics.bi_integrate_recipe_datefunctions.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: bbd84885a2d555b918f776a91d1256676edaf132418574e4babe0db025dbaa5c
description: In CRM Analytics, use date functions to adjust or calculate values from dates in your recipe. For example, you can add time, find time until, and extract a day of the week from date fields. The arguments for date functions are date columns.
products:
  - CRM Analytics
  - Cross Cloud
release: "264"
---
# Date Functions for Formulas

In CRM Analytics, use date functions to adjust or calculate values from dates in your recipe. For example, you can add time, find time until, and extract a day of the week from date fields. The arguments for date functions are date columns.

## Date Functions for Formulas

When entering a formula use the API name of the column in the expression, not the label. The parameters section is surrounded by parentheses. For example, you can enter the following expression.

```
datediff(ClosedDate, CreatedDate)
```

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small ">
        <colgroup>
          <col style="width:22.98850574712644%">
          <col style="width:32.41379310344827%">
          <col style="width:44.59770114942529%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d131461e47">Date Function</th>
            <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d131461e50">Description</th>
            <th scope="col" class="slds-cell-wrap" style="vertical-align:top;" id="d131461e53">Syntax</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>add_months</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the date with the specified number of months after the start
              date.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>add_months(<samp class="parmname">startDate</samp>,<samp class="parmname">num_months</samp>)</code>
              </p>
              <p><samp class="parmname">startDate</samp> is the date field calculated
                on.</p>
              <p><samp class="parmname">num_months</samp> is the number of months added to the
                startDate.</p>
              <p>This function doesn't adjust the resulting date to a last day of
                month if the <samp class="parmname">startDate</samp> is a last day of months. The resulting
                day of the month is the minimum of original date's day or the last day of the
                month of the resulting date. </p>
              <p>For example:</p>
              <ul>
                <li><code>select add_months(DATE'2023-02-28', 1)</code> returns
                  <code>2023-03-28</code>.
                </li>

                <li><code>select add_months(DATE'2023-01-31', 1)</code> returns
                  <code>2023-02-28</code>.
                </li>

                <li><code>select add_months(DATE'2024-01-31', 1)</code> returns
                  <code>2024-02-29</code>, as 2020 is a leap year.
                </li>

              </ul>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>current_date</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the current date. </td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <code>current_date()</code>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>current_timestamp</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the current date and time.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <code>current_timestamp()</code>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>datediff</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the number of days between start date and end date.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>datediff(<samp class="parmname">endDate</samp>,<samp class="parmname">startDate</samp>)</code>
              </p>
              <p><samp class="parmname">endDate</samp> is the later of the two dates you are finding the
                difference between.</p>
              <p style="margin-bottom:0;"><samp class="parmname">startDate</samp> is the earlier of the two
                dates you are finding the difference between.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>date_add</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the date with the specified number of days after the start
              date.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>date_add(<samp class="parmname">startDate</samp>,<samp class="parmname">num_days</samp>)</code>
              </p>
              <p><samp class="parmname">startDate</samp> is the date field calculated
                on.</p>
              <p style="margin-bottom:0;"><samp class="parmname">num_days</samp> is the number of days added to the
                startDate. </p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>date_format</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Converts the timestamp to a specified date format.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>date_format(<samp class="parmname">field</samp>,<samp class="parmname">'format'</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp> is a date field/timestamp or string to be converted to
                the given format.</p>
              <p><samp class="parmname">'format'</samp> is the date and time format
                pattern to follow.</p>
              <div>
                <p>Example:
                </p>
                <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>date_format('2024-04-08', 'y')</code></pre>
                <p> returns 2024</p>
              </div>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>date_sub</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Removes the specified number of days from the start date.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>date_sub(<samp class="parmname">startDate</samp>,<samp class="parmname">num_days</samp>)</code>
              </p>
              <p><samp class="parmname">startDate</samp> is the date field calculated
                on.</p>
              <p style="margin-bottom:0;"><samp class="parmname">num_days</samp> is the number of days subtracted from
                the startDate.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>date_trunc</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Resets the specified part of the timestamp, and all portions after it, to zero
              (or 01 for date and year). Specify the portion of the timestamp, fmt, with ‘year’,
              ‘quarter’, ‘month’, ‘week’, ‘day’, ‘hour’, ‘minute’, or ‘second’. For example,
              dateTrunc for ‘year’ of 2019-05-01 12:31:32 returns 2019-01-01 00:00:00. If your
              data is date and not datetime, use the trunc function instead.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>date_trunc(<samp class="parmname">'format'</samp>,<samp class="parmname">timestamp</samp>)</code>
              </p>
              <p><samp class="parmname">'format'</samp> is the date and time format pattern to
                follow.</p>
              <p><samp class="parmname">timestamp</samp> is a date/timestamp or string
                that’s returned as a UNIX timestamp.</p>
              <div>
                <p>Example:
                </p>
                <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>date_trunc('week',CloseDate)</code></pre>
                <p> returns the Monday of the week
                  of the Close Date.</p>
              </div>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>day</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the day of the month component of the date/timestamp.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>day(<samp class="parmname">date</samp>)</code>
              </p>
              <p style="margin-bottom:0;"><samp class="parmname">date</samp> is the date field to extract the day of the month from.
              </p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>dayofmonth</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the day of the month component of the date/timestamp.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>dayofmonth(<samp class="parmname">date</samp>)</code>
              </p>
              <p style="margin-bottom:0;"><samp class="parmname">date</samp> is the date field to extract the day of the month
                from.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>dayofweek</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the day of the week component of the date/timestamp. 1 = Sunday, 2 =
              Monday, ..., 7 = Saturday.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>dayofweek(<samp class="parmname">date</samp>)</code>
              </p>
              <p style="margin-bottom:0;"><samp class="parmname">date</samp> is the date field to extract the day of the week
                from.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>dayofyear</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the day of year component of the date/timestamp.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>dayofyear(<samp class="parmname">date</samp>)</code>
              </p>
              <p style="margin-bottom:0;"><samp class="parmname">date</samp> is the date field to extract the day of the year
                from.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>months_between</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the number of months between two timestamps. Calculates the difference
              based on 31 days per month rounded to 8 digits unless roundOff=false. If timestamp1
              is later than timestamp2, the result is positive. If timestamp1 and timestamp2 are
              on the same day of the month, or both are the last day of the month, the result is
              an integer and the time of day is ignored.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>months_between(<samp class="parmname">timestamp2</samp>,<samp class="parmname">timestamp1</samp>,<samp class="parmname">roundOff(optional)</samp>)</code>
              </p>
              <p><samp class="parmname">timestamp2</samp> is one of the timestamps to
                compare.</p>
              <p><samp class="parmname">timestamp1</samp> is the other timestamp to
                compare.</p>
              <p style="margin-bottom:0;"><samp class="parmname">roundOff(optional)</samp> is a boolean that
                determines whether to round the result. This parameter is optional.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>now</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the current date and time in the specified format.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <code>now()</code>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>to_date</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Converts the date string to the specified date format. Returns null with
              invalid input.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>to_date(<samp class="parmname">field</samp>,<samp class="parmname">'format(optional)'</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp> is a date string field you want to
                convert.</p>
              <p style="margin-bottom:0;"><samp class="parmname">'format(optional)'</samp> is the date and time
                format pattern to follow. This parameter is optional. The function follows casting
                rules to a date if format isn’t included.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>to_timestamp</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Converts the timestamp string to the specified timestamp format.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>to_timestamp(<samp class="parmname">field</samp>,<samp class="parmname">'format(optional)'</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp> is a timestamp string field you want to
                convert.</p>
              <p style="margin-bottom:0;"><samp class="parmname">'format(optional)'</samp> is the timestamp format
                pattern to follow. This parameter is optional. The function follows casting rules
                to a timestamp if format isn’t included.</p>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>to_unix_timestamp</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Returns the UNIX timestamp of the specified time.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>to_unix_timestamp(<samp class="parmname">timestamp</samp>,<samp class="parmname">format(optional)</samp>)</code>
              </p>
              <p><samp class="parmname">timestamp</samp> is a date/timestamp or string that’s returned as a
                UNIX timestamp.</p>
              <p><samp class="parmname">format(optional)</samp> is the date/time
                format pattern to follow. <samp class="parmname">format(optional)</samp> is ignored if
                <samp class="parmname">timestamp</samp> is not a string. Default value is "yyyy-MM-dd
                HH:mm:ss". This parameter is optional.
              </p>
              <div>
                <p>Example:
                </p>
                <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>to_unix_timestamp('2016-04-08', 'yyyy-MM-dd')</code></pre>
                <p> returns
                </p>
                <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>1460098800</code></pre>
              </div>
            </td>

          </tr>
          <tr class="">
            <td data-label="Date Function" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e47 ">
              <code>trunc</code>
            </td>

            <td data-label="Description" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e50 ">Replaces the specified portion of the timestamp, and all portions after, with
              zeroes.</td>

            <td data-label="Syntax" class="slds-cell-wrap" style="vertical-align:top;" headers="d131461e53 ">
              <p>
                <code>trunc(<samp class="parmname">field</samp>,<samp class="parmname">'format(optional)'</samp>)</code>
              </p>
              <p><samp class="parmname">field</samp> is a timestamp
                field.</p>
              <p><samp class="parmname">'format(optional)'</samp> specifies the part of the
                timestamp to truncate. Accepted values are "YEAR", "YYYY", "YY", "MON", "MONTH",
                "MM", "DAY", "DD", "HOUR", "MINUTE", "SECOND", "WEEK", and
                "QUARTER".</p>
              <div>
                <p>Example:
                </p>
                <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>trunc('2024-03-05T09:32:05.359', 'MM')</code></pre>
                <p> returns
                </p>
                <pre class="line-numbers slds-box slds-box_x-small slds-theme_shade slds-m-top_medium slds-m-bottom_medium slds-scrollable_x"><code>2024-03-01T00:00:00</code></pre>
              </div>
            </td>

          </tr>
        </tbody>
      </table>

Keep these behaviors in mind when working with date and time functions.

- These functions are relative to when the job runs.
    - `now`
    - `current_date`
    - `current_timestamp`
    - `to_unix_timestamp`
- All date and datetime functions are based on the UTC time zone.
- When Custom Time Zone Support is enabled, date functions and date-time functions are separated. When it’s disabled, the functions are grouped together.