Number of months between two dates

This seems like a pretty simple and common requirement but the truth is, it is a common requirement but it is simple only if you know the solution.

Requirement:

You have a Start date and an End date. You need to find number of months between the two dates.

Assumption:

We do not exclude partial months or in other words we are not considering number of days in a month. Below are few examples to help in understanding what we are trying to achieve

Start Date : 01/01/2011 End Date : 03/15/2011   ::  Number of months = 3
Start Date : 01/28/2011 End Date : 02/01/2011   ::  Number of months = 2
Start Date : 01/01/2011 End Date : 01/15/2011   ::  Number of months = 1

Declarative Solution:

The Declarative solution is pretty simple and can be used anywhere Siebel supports expressions i.e Calculated Fields, Workflows, Runtime Events, Data Validation Manager, EAI Data Maps etc. I am providing solution by way of Calculated Field but the same expression can by used on any of the objects mentioned above

Calculated Field ExpressionJulianMonth([End Date]) - JulianMonth([Start Date]) + 1

Scripting Solution:

Sometimes scripting is the only available interface for you (business service supporting integration) and it is not possible to use declarative solution. In that case the function JulianMonth is not available to you. You can use following line of code to accomplish the same

var numOfMonth = (endDate.getYear() - startDate.getYear()) * 12 + endDate.getMonth() - startDate.getMonth() + 1 ;

I will post the solution to only consider whole months depending on the interest. Let me know by way of comments if you would like to see the solution to exclude partial months.

Your feedback is welcome as always.

3 Responses to Number of months between two dates

  1. Hi Neel,

    Nice post , i am new in siebel, can u please tell me that, can we query by date in UI field(ex – if i want to query in UI field the dates which have months 02 only, so can i query as 12*) , my doubt is if we query like this, how the siebel will recognise that we are trying to query date or month. Please clarify.

    Thanks

  2. Can you post as to how find the number of working days between two given dates, given that you ahve to exclude weeknds and holidays as mentioned in the Schedule please

    • hi even am looking for the same type of date calculation which excludes weekends and holidays can you please help me

Leave a Reply

Contribute