Activity Forums Salesforce® Discussions How to get start date and end date with time in single function from calendar in Salesforce?

  • Archit

    Member
    March 28, 2018 at 2:43 pm

    Hello Amresh,

    We can achieve it by Salesforce standard functionality so please follow below steps to do so.

    • Create Date field on any object name "Start Date"
    • Create Date field on same object name "End Date"
    • Create Formula field on same object name "Number of days"
    • make sure the return type of formula field should be number.
    • after creating record you can achieve what you want.

    Thanks

  • Neha

    Member
    March 29, 2018 at 6:03 am

    Hi Amresh,

    Do you want to calculate the no. of days between the start date and end date of date time type? If yes, then from calendar you can get and start date and end date in variable

    If the total duration is less than 24 hours, the following code works in javascript

    var now = "04/09/2013 15:00:00";
    var then = "04/09/2013 14:20:30";

    moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")
    This approach will work ONLY when the total duration is less than 24 hours:

    var now = "04/09/2013 15:00:00";
    var then = "04/09/2013 14:20:30";

    moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

    // outputs: "00:39:30"

    If the response for durations of 24 hours or greater, then following code works in javascript

    var now = "04/09/2013 15:00:00";
    var then = "02/09/2013 14:20:30";

    var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
    var d = moment.duration(ms);
    var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

    // outputs: "48:39:30"

  • Parul

    Member
    September 20, 2018 at 6:44 pm

    Hi,

    Please follow the formula to get the achieve the functionality

    DATE( Year([start date])+floor((MONTH([start date]) + [# of months] - 1) / 12) ,
    mod(MONTH([start date]) + [# of months] -1, 12) + 1 ,
    day([start date])
    ) - 1

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos