Question

I cannot seem to get my code to calculate the amount of minutes between 2 datetime fields:

private void button4_Click(object sender, EventArgs e)
{
    string startTime = "5/1/2008 1:00:00 PM";
    string endTime = "5/1/2008 3:00:00 PM";

    DateTime startTimeParse = 
        DateTime.Parse(startTime, CultureInfo.InvariantCulture);
    DateTime endTimeParse = 
        DateTime.Parse(endTime, CultureInfo.InvariantCulture);

    MessageBox.Show(startTime);
    MessageBox.Show(endTime);

    TimeSpan result = endTimeParse - startTimeParse;
    int hours = result.Hours;
    int minutes = result.Minutes;
}

In debug the result is just 00:00:00

If you can show me how to MessageBox.show the different in mins?

Was it helpful?

Solution

Math between two DateTime values will produce a TimeSpan value. So you can just do like this:

(endTimeParse - startTimeParse).TotalMinutes;

OTHER TIPS

Based on your last comment, try:

MessageBox.Show(result.TotalMinutes.ToString())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top