Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

How to calculate the difference between a date field and the companies year end

(0) ShareShare
ReportReport
Posted on by

Hello, 

I am looking for a way to calculate the difference between a start date and our year-end date.  Has anyone come across a requirement and a solution to calculate the difference between a date and the companies year-end?  I was thinking of using a hidden field that would set the year-end on the creation of a record, but this approach would require a batch job to ensure the year-end date is correct. 

Thank you

  • Inogic Profile Picture
    Inogic 402 on at
    RE: How to calculate the difference between a date field and the companies year end

    Hi,

    As per the question it is not clear whether you have a constant year end date value for your company or different end dates for different companies.
    In case you have a constant year end date value for your company then you don’t need to have a separate Date Time field to store the end date, instead you can directly use the hardcoded value to find the difference between the Start Date and End Date programmatically either using C# or JavaScript.

    Using C# code.

    You can use the below code to find difference between two dates.

    • Difference in Days.

    int numOfPassedInvoices = (endDate - startDate).TotalDays;

    • Difference in Weeks

    (Use Math.Truncate only if you do not consider the current period and if you want to consider the current period use Math.Ceiling)

    int numOfPassedInvoices = Convert.ToInt32(Math.Truncate((endDate - startDate).TotalDays / 7));

    • Difference in Months

    int numOfPassedInvoices = Convert.ToInt32(Math.Truncate((endDate - startDate).TotalDays / 30.4));

    • Difference in Years

    int numOfPassedInvoices = Convert.ToInt32(Math.Truncate((endDate - startDate).TotalDays / 365));


    Using JavaScript.

    The below code is to find the year difference.

    var startDate = new Date();
    var endDate = new Date("01/01/2023");
    var diffDays = Math.ceil( parseFloat((endDate - startDate) / (1000 * 60 * 60 * 24 * 365), 10));

    To find difference in days remove the 365.
    To find difference in weeks replace the 365 with 7.
    To find difference in Months replace the 365 with 30.4.

    However if you do not have the constant end date value and you have different companies records with different year end, in that case you need to have a separate field to store the End Date.
    Now to find the difference you can create a Calculated DateTime fields as shown below.

    The calculated field will provide multiple type of function to find difference in Days, Weeks, Months and Years etc.

    set-date-difference.jpg

    In case you do not want to have the calculated field then you can use the C# or JavaScript code provided above.

    Hope this helps.

    Thanks!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to calculate the difference between a date field and the companies year end

    Hi wetherington,

    I'm not clear about your question.

    Do you have two date fields in your entity, start date and year-end date? And you want to count the days from start date to year-end date?

    Could you give some sample date and your expect output?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans