web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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

I have the same question (0)
  • Community Member Profile Picture
    on at

    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?

  • Inogic Profile Picture
    703 on at

    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!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans