# How to Calculate the Difference Between two Dates

Respected Members,

I want to calculate the length2 in the below code which is the difference between lastbardate and a paramdate.

I referred the function DateTimeDiff( arg1, arg2 ) but not able to understand the inputs of arg1 and arg2 any help is much appreciated.

``````_SECTION_BEGIN("Comparison");

comparativeTickerId = ParamStr( "Comparative Symbol", "Nifty 50");
length1 = Param( "Period", 310, 2, 200, 1 );

**length2 = Difference between  Lastbardate and  ParamDate("Date","01-01-2020",0);**
( 11-05-2021 - 01-01-2020)

Plot( 0, "Zero Line", colorRed, styleDashed );

//Current Symbol Gain-1
closeGain1 = C / Ref (C, -length1);
//Base Ticker Gain-1
SetForeign(comparativeTickerId);
baseGain1 = C / Ref (C, -length1);
RestorePriceArrays();

//Current Symbol Gain-2
closeGain2 = C / Ref (C, -length2);
//Base Ticker Gain-2
SetForeign(comparativeTickerId);
baseGain2 = C / Ref (C, -length2);
RestorePriceArrays();

//Line
res1 = closeGain1/baseGain1 -1;
res2 = closeGain2/baseGain2 -1;

Plot( res1, "Res1", IIf(res1>0,colorGreen,colorRed), styleLine | styleThick );
Plot( res2, "Res1", IIf(res2>0,colorGreen,colorRed), styleLine | styleThick );

GraphLabelDecimals = 2;

_SECTION_END();

``````

Thank you

DateTimeDiff `arg1` and `arg2` have to be type `DateTime`.

DateTimeDiff( arg1, arg2 ) which will return positive values if arg1 > arg2 and negative values if arg1 < arg2. The difference is given in seconds.

So ParamDate's 3rd argument has to be value `2`.

• 2 - return value is a NUMBER and holds DateTime. (new in 6.20)

Last DateTime is `LastValue(DateTime())`;
Output of DateTImeDiiff is seconds so to get number of days divide by `inDaily`.

``````start_date = ParamDate("Date","01-01-2020", 2 );
last_dt = LastValue(DateTime());

dt_diff = DateTimeDiff(last_dt, start_date) / inDaily;

printf("Date Diff (days): %g", dt_diff);
``````
4 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.