Concepts or coding lessons of Salesforce that you can implement easily

Simplest Way To Find Number of Days Between Two Dates in Salesforce

To Find Number of Days Between Two Dates in Salesforce, it provides daysBetween() function which returns the number of days between the Date that called the method and the specified date.

For Example:


Date startDate  = Date.valueOf('2016-05-12'); // 12th May 2016
Date endDate   = Date.valueOf('2016-05-30'); // 30th May 2016

Integer noOfDaysBetweenDates = startDate.daysBetween(endDate);

System.debug('No Of Days--- '+noOfDaysBetweenDates);

Output : 

No Of Days--- 18

Some other Date methods which can help you in other requirements.

The following are methods for Date :


addDays(additionalDays) :
This function adds the specified number of additional days to a Date.
Syntax : public Date addDays(Integer additionalDays)

addMonths(additionalMonths) :
This function adds the specified number of additional months to a Date .
Syntax : public Date addMonths(Integer additionalMonths)

addYears(additionalYears) :
This function adds the specified number of additional years to a Date .
Syntax : public Date addYears(Integer additionalYears)

day() :
This function returns the day-of-month component of a Date .
Syntax : public Integer day()

dayOfYear() :
This function returns the day-of-year component of a Date .

daysBetween(secondDate) :
This function returns the number of days between the Date that called the method and the specified date.

daysInMonth(year, month) :
This function returns the number of days in the month for the specified year and month (1=Jan) .

format() :
This function returns the Date as a string using the locale of the context user .

isLeapYear(year) :
This function returns true if the specified year is a leap year .

isSameDay(dateToCompare) :
This function returns true if the Date that called the method is the same as the specified date .

month() :
This function returns the month component of a Date (1=Jan) .

monthsBetween(secondDate) :
This function returns the number of months between the Date that called the method and the specified date, ignoring the difference in days .

newInstance(year, month, date) :
Constructs a Date from Integer representations of the year, month (1=Jan), and day .

parse(stringDate) :
Constructs a Date from a String. The format of the StringDate depends on the local date format .

today() :
This function returns the current date in the current user's time zone .

toStartOfMonth() :
This function returns the first of the month for the Date that called the method .

toStartOfWeek() :
This function returns the start of the week for the Date that called the method, depending on the context user's locale .

valueOf(stringDate) :
This function returns a Date that contains the value of the specified String .

valueOf(fieldValue) :
Converts the specified object to a Date. Use this method to convert a history tracking field value or an object that represents a Date value .

year() :

This function returns the year component of a Date .

Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

loading...

No comments:

Post a Comment