Situation
You want to calculate the time difference between two bars in MultiCharts .NET.

Programming example

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    [SameAsSymbol(true)]
    public class Example_TimeDifferenceBetweenBars : IndicatorObject
    {
        public Example_TimeDifferenceBetweenBars(object _ctx) : base(_ctx) { }

        protected override void CalcBar()
        {
            // Output the data on the last bar
            if (Bars.LastBarOnChart)
            {
                for (int i = 1; i < 5; i++)
                {
                    OutputTimeDifference(Bars.Time[0], Bars.Time[i * 30]);
                }
            }
        }

        // Output the time difference between two DateTimes
        private void OutputTimeDifference(DateTime recentDateTime, DateTime oldDateTime)
        {
            // Calculate the time difference, a TimeSpan, by subtracting
            // the 'oldDateTime' from the 'recentDateTime'
            TimeSpan timeDiff = recentDateTime.Subtract(oldDateTime);

            // Output the DateTimes for comparison purposes
            Output.WriteLine("Recent DateTime:\t{0}{1}Older DateTime:\t{2}",
                recentDateTime.ToString("dd-MM-yy HH:mm:ss"),
                System.Environment.NewLine,
                oldDateTime.ToString("dd-MM-yy HH:mm:ss"));

            // Output the total time differences
            Output.WriteLine("Time difference: {0}. Total hours: {1}. Total minutes: {2}. Total seconds: {3}.",
                timeDiff,
                timeDiff.TotalHours,
                timeDiff.TotalMinutes,
                timeDiff.TotalSeconds);

            // Ouput the time difference with the individual components
            Output.WriteLine("The time difference is {0} hours, {1} minutes, and {2} seconds.{3}",
                timeDiff.Hours,
                timeDiff.Minutes,
                timeDiff.Seconds,
                System.Environment.NewLine);
        }
    }
}

Output of the programming example

The output of the example looks something like:

Recent DateTime:    25-10-13 10:30:00
Older DateTime:     25-10-13 08:00:00
Time difference: 02:30:00. Total hours: 2,5. Total minutes: 150. Total seconds: 9000.
The time difference is 2 hours, 30 minutes, and 0 seconds.

Recent DateTime:    25-10-13 10:30:00
Older DateTime:     25-10-13 05:30:00
Time difference: 05:00:00. Total hours: 5. Total minutes: 300. Total seconds: 18000.
The time difference is 5 hours, 0 minutes, and 0 seconds.

Recent DateTime:    25-10-13 10:30:00
Older DateTime:     25-10-13 03:00:00
Time difference: 07:30:00. Total hours: 7,5. Total minutes: 450. Total seconds: 27000.
The time difference is 7 hours, 30 minutes, and 0 seconds.

Recent DateTime:    25-10-13 10:30:00
Older DateTime:     25-10-13 00:30:00
Time difference: 10:00:00. Total hours: 10. Total minutes: 600. Total seconds: 36000.
The time difference is 10 hours, 0 minutes, and 0 seconds.

MultiCharts .NET programming example

To calculate the time difference between two price bars, we need to use the Bars.Time[x] property, which returns the DateTime value for the specified bar. Working with date and time values in C# is done with the DateTime structure, which enables you to create values that represent a date and a time, just a date, or just a time (Dorman, 2010). When we add or subtract two DateTime values, we end up with a TimeSpan structure, which is a positive or negative time interval.

In this example, the method OutputTimeDifference() is called in the CalcBar() override method:

// Output the data on the last bar
if (Bars.LastBarOnChart)
{
    for (int i = 1; i < 5; i++)
    {
        OutputTimeDifference(Bars.Time[0], Bars.Time[i * 30]);
    }
}

The if statement checks to see if the current bar is the last bar on the chart, in which case Bars.LastBarOnChart will return true. In the for loop the OutputTimeDifference() method is called. This methods takes two parameters: the recent DateTime, for which we used the DateTime of the current bar (Bars.Time[0]), and an older date and time of a previous bar (i.e. Bars.Time[i * 30]).

Calculating the time difference between MultiCharts .NET bars

The implementation of the OutputTimeDifference() methods is as follows:

// Output the time difference between two DateTimes
private void OutputTimeDifference(DateTime recentDateTime, DateTime oldDateTime)
{
    // Calculate the time difference, a TimeSpan, by subtracting
    // the 'oldDateTime' from the 'recentDateTime'
    TimeSpan timeDiff = recentDateTime.Subtract(oldDateTime);

    // Output the DateTimes for comparison purposes
    Output.WriteLine("Recent DateTime:\t{0}{1}Older DateTime:\t{2}",
        recentDateTime.ToString("dd-MM-yy HH:mm:ss"),
        System.Environment.NewLine,
        oldDateTime.ToString("dd-MM-yy HH:mm:ss"));

    // Output the total time differences
    Output.WriteLine("Time difference: {0}. Total hours: {1}. Total minutes: {2}. Total seconds: {3}.",
        timeDiff,
        timeDiff.TotalHours,
        timeDiff.TotalMinutes,
        timeDiff.TotalSeconds);

    // Ouput the time difference with the individual components
    Output.WriteLine("The time difference is {0} hours, {1} minutes, and {2} seconds.{3}",
        timeDiff.Hours,
        timeDiff.Minutes,
        timeDiff.Seconds,
        System.Environment.NewLine);
}

This method starts with declaring a TimeSpan type which holds the time difference between the two DateTime values. This subtraction is done with the Subtract method of the DateTime type, whereby we subtract the older DateTime from the recent one to arrive at the positive time difference.

Next both DateTime values are outputted to the PowerLanguage Editor output tab. With the System.Environment.NewLine a new line is generated in the output.

In the second part of the method, the total time difference in hours, minutes, and seconds is displayed. This is done by respectively the TotalHours, TotalMinutes, and TotalSeconds properties of the TimeSpan type.

In the last part of the OutputTimeDifference() method, the individual components of the time difference are outputted. For this the Hours, Minutes, and Seconds properties are used.

You can also use a TimeSpan to get the number of days, milliseconds, and even ticks between two DateTime values by using the corresponding properties.

So to calculate the time difference between two bars: subtract both DateTime values and save this into a TimeSpan structure. Then use the TimeSpan’s properties to retrieve the time difference.

References

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN (USA): Sams/Pearson Education.