Newer Version Available
Sample Date Formulas
| Available in: both Salesforce Classic and Lightning Experience |
| Available in: All Editions |
Find the Day, Month, or Year from a Date
Use the functions DAY( date ), MONTH( date ), and YEAR( date ) to return their numerical values. Replace date with a value of type Date (for example, TODAY()).
To use these functions with Date/Time values, first convert them to a date with the DATEVALUE() function. For example, DAY( DATEVALUE( date/time )).
Find Out If a Year Is a Leap Year
1OR(
2 MOD( YEAR( date ), 400 ) = 0,
3 AND(
4 MOD( YEAR( date ), 4 ) = 0,
5 MOD( YEAR( date ), 100 ) != 0
6 )
7)Find Which Quarter a Date Is In
For standard quarters, you can determine which quarter a date falls in using this formula. This formula returns the number of the quarter that date falls in (1–4) by dividing the current month by three (the number of months in each quarter) and taking the ceiling.
1CEILING( MONTH ( date ) / 3 )1CEILING( ( MONTH ( date ) - 1 ) / 3)1AND(
2 CEILING( MONTH( date ) / 3 ) = CEILING( MONTH( TODAY() ) / 3 ),
3 YEAR( date ) = YEAR( TODAY() )
4)Find the Week of the Year a Date Is In
1IF(
2 CEILING( ( date - DATE( YEAR( date ), 1, 1) + 1) / 7) > 52,
3 52,
4 CEILING( ( date - DATE( YEAR( date ), 1, 1) + 1) / 7)
5)Find Whether Two Dates Are in the Same Month
1AND(
2 MONTH( date_1 ) == MONTH( date_2 ),
3 YEAR( date_1 ) == YEAR( date_2 )
4)Find the Last Day of the Month
1IF(
2 MONTH( date ) = 12,
3 DATE( YEAR( date ), 12, 31 ),
4 DATE( YEAR( date ), MONTH ( date ) + 1, 1 ) - 1
5)Display the Month as a String instead of a Number
1CASE(
2 MONTH( date ),
3 1, "January",
4 2, "February",
5 3, "March",
6 4, "April",
7 5, "May",
8 6, "June",
9 7, "July",
10 8, "August",
11 9, "September",
12 10, "October",
13 11, "November",
14 "December"
15)1CASE(
2 MONTH( date ),
3 1, $Label.Month_of_Year_1,
4 2, $Label.Month_of_Year_2,
5 3, $Label.Month_of_Year_3,
6 4, $Label.Month_of_Year_4,
7 5, $Label.Month_of_Year_5,
8 6, $Label.Month_of_Year_6,
9 7, $Label.Month_of_Year_7,
10 8, $Label.Month_of_Year_8,
11 9, $Label.Month_of_Year_9,
12 10, $Label.Month_of_Year_10,
13 11, $Label.Month_of_Year_11,
14 $Label.Month_of_Year_12
15)Find and Display the Day of the Week from a Date
1CASE(
2 MOD( date - DATE( 1900, 1, 7 ), 7 ),
3 0, "Sunday",
4 1, "Monday",
5 2, "Tuesday",
6 3, "Wednesday",
7 4, "Thursday",
8 5, "Friday",
9 "Saturday"
10)1CASE(
2 MOD( date - DATE( 1900, 1, 8 ), 7 ),
3 0, "Monday",
4 1, "Tuesday",
5 2, "Wednesday",
6 3, "Thursday",
7 4, "Friday",
8 5, "Saturday",
9 "Sunday"
10)To get the formula for 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, and so on.
Find the Next Day of the Week After a Date
1date + ( day_of_week - MOD( date - DATE( 1900, 1, 7 ), 7 ) )
2+
3IF(
4 MOD( date - DATE( 1900, 1, 7 ), 7 ) >= day_of_week,
5 7,
6 0
7)Find the Number of Days Between Two Dates
To find the number of days between two dates, date_1, and date_2, subtract the earlier date from the later date: date_1 — date_2
1TODAY() - 30 > dateFind the Number of Weekdays Between Two Dates
1(5 * ( FLOOR( ( date_1 - DATE( 1900, 1, 8) ) / 7 ) ) + MIN( 5, MOD( date_1 - DATE( 1900, 1, 8), 7 ) ) )
2-
3(5 * ( FLOOR( ( date_2 - DATE( 1900, 1, 8) ) / 7 ) ) + MIN( 5, MOD( date_2 - DATE( 1900, 1, 8), 7 ) ) )Find the Number of Months Between Two Dates
To find the number of months between two dates, subtract the year of the earlier date from the year of the later date and multiply the difference by 12. Next, subtract the month of the earlier date from the month of the later date, and add that difference to the value of the first set of operations.
1((YEAR(date_1) - YEAR(date_2))*12) + (MONTH(date_1) - MONTH(date_2))Add Days, Months, and Years to a Date
If you want to add a certain number of days to a date, add that number to the date directly. For example, to add 5 days to a date, the formula is date + 5.
1ADDMONTHS()1ADDMONTHS(date + 4)1ADDMONTHS(date, 12*num_years)1IF( MOD((Year (ADDMONTHS(date, 12* num_years)-1960),4)=0, ADDMONTHS(date,12* num_years)+1,ADDMONTHS(date, 12*num_years))Add Business Days to a Date
1CASE(
2 MOD( date - DATE( 1900, 1, 7 ), 7 ),
3 3, date + 2 + 3,
4 4, date + 2 + 3,
5 5, date + 2 + 3,
6 6, date + 1 + 3,
7 date + 3
8)Find the Hour, Minute, or Second from a Date/Time
1VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) )1IF(
2 OR(
3 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 0,
4 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 12
5 ),
6 12,
7 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) )
8 -
9 IF(
10 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
11 0,
12 12
13 )
14)1VALUE( MID( TEXT( date/time - TZoffset ), 15, 2 ) )1VALUE( MID( TEXT( date/time - TZoffset ), 18, 2 ) )1IF(
2 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
3 "AM",
4 "PM"
5)1IF(
2 OR(
3 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 0,
4 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) = 12
5 ),
6 "12",
7 TEXT( VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) )
8 -
9 IF(
10 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
11 0,
12 12
13 )
14 )
15)
16& ":" &
17MID( TEXT( date/time - TZoffset ), 15, 2 )
18& ":" &
19MID( TEXT( date/time - TZoffset ), 18, 2 )
20& " " &
21IF(
22 VALUE( MID( TEXT( date/time - TZoffset ), 12, 2 ) ) < 12,
23 "AM",
24 "PM"
25)Find the Elapsed Time Between Date/Times
To find the difference between two Date values as a number, subtract one from the other like so: date_1 — date_2 to return the difference in days.
1IF(
2 datetime_1 - datetime_2 > 0 ,
3 TEXT( FLOOR( datetime_1 - datetime_2 ) ) & " days "
4 & TEXT( FLOOR( MOD( (datetime_1 - datetime_2 ) * 24, 24 ) ) ) & " hours "
5 & TEXT( ROUND( MOD( (datetime_1 - datetime_2 ) * 24 * 60, 60 ), 0 ) ) & " minutes",
6 ""
7)Find the Number of Business Hours Between Two Date/Times
1ROUND( 8 * (
2 ( 5 * FLOOR( ( DATEVALUE( date/time_1 ) - DATE( 1900, 1, 8) ) / 7) +
3 MIN(5,
4 MOD( DATEVALUE( date/time_1 ) - DATE( 1900, 1, 8), 7) +
5 MIN( 1, 24 / 8 * ( MOD( date/time_1 - DATETIMEVALUE( '1900-01-08 16:00:00' ), 1 ) ) )
6 )
7 )
8 -
9 ( 5 * FLOOR( ( DATEVALUE( date/time_2 ) - DATE( 1900, 1, 8) ) / 7) +
10 MIN( 5,
11 MOD( DATEVALUE( date/time_2 ) - DATE( 1996, 1, 1), 7 ) +
12 MIN( 1, 24 / 8 * ( MOD( date/time_2 - DATETIMEVALUE( '1900-01-08 16:00:00' ), 1) ) )
13 )
14 )
15 ),
160 )