Using Date and Date/Time in Formulas
Using Date and Date/Time in Formulas
Formulas
Salesforce, Spring ’18
@salesforcedocs
Last updated: February 3, 2018
© Copyright 2000–2018 salesforce.com, inc. All rights reserved. Salesforce is a registered trademark of salesforce.com, inc.,
as are other names and marks. Other marks appearing herein may be trademarks of their respective owners.
CONTENTS
Date formulas are useful for managing payment deadlines, contract ages, or any other features of
EDITIONS
your organization that are time or date dependent.
Two data types are used for working with dates: Date and Date/Time. Most values that are used Available in: both Salesforce
when working with dates are of the Date data type, which store the year, month, and day. Some Classic and Lightning
fields, such as CreatedDate, are Date/Time fields, meaning they not only store a date value, Experience
but also a time value (stored in GMT but displayed in the users’ time zone). Date and Date/Time Available in: All Editions
fields are formatted in the user’s locale when viewed in reports and record detail pages.
You can use operations like addition and subtraction on Date and Date/Time values to calculate a
future date or elapsed time between two dates. If you subtract one date from another, for example, the resulting value will be the
difference between the two initial values in days (Number data type). The same operation between two Date/Time values returns a
decimal value indicating the difference in number of days, hours, and minutes.
For example, if the difference between two Date/Time values is 5.52, that means the two values are separated by five days, 12 hours (0.5
of a day), and 28 minutes (0.02 of a day). You can also add numeric values to Dates and Date/Times. For example, the operation TODAY()
+ 3 returns three days after today’s date. For more information and examples of working with dates, see the list of Sample Date
Formulas.
Throughout the examples, the variables date and date/time are used in place of actual Date and Date/Time fields or values.
Keep in mind that complex date functions tend to compile to a larger size than text or number formula functions, so you might run into
issues with formula compile size. See Tips for Reducing Formula Size for help with this problem.
1
Using Date and Date/Time Values in Formulas
This returns the date in the format “YYYY-MM-DD” rather than in the locale-dependent format. You can change the format by extracting
the day, month, and year from the date first and then recombining them in the format you want. For example:
"Today's date is " & TEXT( MONTH( date ) ) & "/" & TEXT( DAY( date ) ) & "/" & TEXT( YEAR(
date ) ) )
You can also convert text to a Date so you can use the string value with your other Date fields and formulas. You’ll want your text to be
formatted as “YYYY-MM-DD”. Use this formula to return the Date value:
DATEVALUE( "YYYY-MM-DD" )
In this formula, NOW() is offset to GMT. Normally, NOW() would be converted to the user’s time zone when viewed, but because it’s
been converted to text, the conversion won’t happen. So if you execute this formula on August 1st at 5:00 PM in San Francisco time
(GMT-7), the result is “The current date and time is 2013–08–02 00:00:00Z”.
When you convert a Date/Time to text, a “Z” is included at the end to indicate GMT. TEXT( date/time ) returns “Z” if the field is
blank. So if the Date/Time value you’re working with might be blank, check for this before converting to text:
IF(
ISBLANK( date/time ),
"",
TEXT( date/time )
)
2
Using Date and Date/Time Values in Formulas
To convert a string to a Date/Time value, use DATETIMEVALUE() passing in a string in the format “YYYY-MM-DD HH:MM:SS”. This
method returns the Date/Time value in GMT.
In the calculation, NOW() is 2013–08–01 19:00:00Z, and then subtracted from 2013–08–02 07:00:00Z, to return the expected result of
0.5 (12 hours).
Suppose that instead of NOW(), the formula converts the string “2013–08–01 12:00:00” to a Date/Time value:
Date_Time_c - DATETIMEVALUE( "2013-08-01 12:00:00" )
In this case, DATETIMEVALUE( “2013–08–01 12:00:00” ) is 2013–08–01 12:00:00Z, and returns a result of 0.79167, or
19 hours.
There’s no way to determine a user’s time zone in a formula. If all of your users are in the same time zone, you can adjust the time zone
difference by adding or subtracting the time difference between the users’ time zone and GMT to your converted values. However, since
time zones can be affected by Daylight Saving Time, and the start and end dates for DST are different each year, this is difficult to manage
in a formula. We recommend using Apex for transactions that require converting between Date/Time values and Text or Date values.
3
SAMPLE DATE FORMULAS
The formula for shifted quarters is similar, but shifts the month of the date by the number of months between January and the first
quarter of the fiscal year. The example below illustrates how you can find a date’s quarter if Q1 starts in February instead of January.
If you want to check whether a date is in the current quarter, add a check to compare the date’s year and quarter with TODAY()’s year
and quarter.
AND(
CEILING( MONTH( date ) / 3 ) = CEILING( MONTH( TODAY() ) / 3 ),
YEAR( date ) = YEAR( TODAY() )
)
4
Sample Date Formulas
You can find the current week by determining how many days there have been in the current year and dividing that value by 7. The
IF() statement ensures that the week number the formula returns doesn’t exceed 52. So if the given date is December 31 of the given
year, the formula returns 52, even though it’s more than 52 weeks after the week of January.
5
Sample Date Formulas
10, "October",
11, "November",
"December"
)
If your organization uses multiple languages, you can replace the names of the month with a custom label:
CASE(
MONTH( date ),
1, $Label.Month_of_Year_1,
2, $Label.Month_of_Year_2,
3, $Label.Month_of_Year_3,
4, $Label.Month_of_Year_4,
5, $Label.Month_of_Year_5,
6, $Label.Month_of_Year_6,
7, $Label.Month_of_Year_7,
8, $Label.Month_of_Year_8,
9, $Label.Month_of_Year_9,
10, $Label.Month_of_Year_10,
11, $Label.Month_of_Year_11,
$Label.Month_of_Year_12
)
Note that this formula only works for dates after 01/07/1900. If you’re working with older dates, use the same process with any Sunday
prior to your earliest date (e.g. 01/05/1800).
You can also adjust this formula if your week starts on a different day. For example, if your week starts on Monday, you can use January
8, 1900 in your condition. The new formula looks like this:
CASE(
MOD( date - DATE( 1900, 1, 8 ), 7 ),
0, "Monday",
1, "Tuesday",
2, "Wednesday",
3, "Thursday",
4, "Friday",
6
Sample Date Formulas
5, "Saturday",
"Sunday"
)
Like the formula for getting the name of the month, if your organization uses multiple languages, you can replace the names of the day
of the week with a variable like $Label.Day_of_Week_1, etc.
You can substitute either a constant or another field in for the day_of_week value based on your needs.
In this formula, date_1 is the more recent date and date_2 is the earlier date. If your work week runs shorter or longer than five
days, replace all fives in the formula with the length of your week.
7
Sample Date Formulas
Adding months to a date is slightly more complicated as months vary in length and the cycle of months restart with each year. Therefore,
a valid day in one month (January 31) might not be valid in another month (February 31). A simple solution is to approximate each
month’s length as 365/12 days:
While this formula is a good estimate, it doesn’t return an exact date. For example, if you add two months to April 30 using this method,
the formula will return June 29 instead of June 30. Returning an exact date depends on your organization’s preference. For example,
when you add one month to January 31, should it return February 28 (the last day of the next month) or March 2 (30 days after January
31)?
This formula does the following:
• Returns March 1 if the future month is a February and the day is greater than 28. This portion of the formula performs the same for
both leap and non-leap years.
• Returns the first day of the next month if the future month is April, June, September, or November and the day is greater than 30.
• Otherwise, it returns the correct date in the future month.
This example formula adds two months to a given date. You can modify the conditions on this formula if you prefer different behaviors
for dates at the end of the month.
DATE(
YEAR( date ) + FLOOR( ( MONTH ( date ) + 2 - 1 ) / 12 ),
MOD( MONTH ( date ) + 2 - 1 +
IF( DAY ( date ) > CASE( MOD( MONTH( date ) + 2 - 1, 12 ) + 1,
2, 28,
8
Sample Date Formulas
4, 30,
6, 30,
9, 30,
11, 30,
31 ), 1, 0 ), 12 ) + 1,
IF( DAY( date ) > CASE( MOD( MONTH( date ) + 2 - 1, 12 ) + 1,
2, 28,
4, 30,
6, 30,
9, 30,
11, 30,
31 ),
1, DAY( date )
)
)
If you’re using these formulas for expiration dates, you might want to subtract a day from the return value to make sure that some action
is completed before the calculated date.
This formula finds the day of the week of the date field value. If the date is a Wednesday, Thursday, or Friday, the formula adds five
calendar days (two weekend days, three weekdays) to the date to account for the weekend. If date is a Saturday, you need four
additional calendar days. For any other day of the week (Sunday — Tuesday), simply add three days. You can easily modify this formula
to add more or less business days. The tip for getting the day of the week might be useful if you need to adjust this formula.
9
Sample Date Formulas
For minutes:
For seconds:
To return the time as a string in “HH:MM:SS A/PM” format, use the following formula:
IF(
OR(
VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 0,
VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 12
),
"12",
TEXT( VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) )
-
IF(
VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
0,
12
)
)
)
& ":" &
MID( TEXT( date/time - TZoffset ), 15, 2 )
& ":" &
MID( TEXT( date/time - TZoffset ), 18, 2 )
& " " &
IF(
VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
"AM",
"PM"
)
When working with time in formula fields, you need to consider the time difference between your organization and GMT. See A Note
About Date/Time and Time Zones on page 3 for help understanding the time zone offset used in this formula.
10
Sample Date Formulas
IF(
datetime_1 - datetime_2 > 0 ,
TEXT( FLOOR( datetime_1 - datetime_2 ) ) & " days "
& TEXT( FLOOR( MOD( (datetime_1 - datetime_2 ) * 24, 24 ) ) ) & " hours "
& TEXT( ROUND( MOD( (datetime_1 - datetime_2 ) * 24 * 60, 60 ), 0 ) ) & " minutes",
""
)
You can change the eights in the formula to account for a longer or shorter work day. If you live in a different time zone or your work
day doesn’t start at 9:00 a.m., change the reference time to the start of your work day in GMT. See A Note About Date/Time and Time
Zones for more information.
11