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?

有帮助吗?

解决方案

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

(endTimeParse - startTimeParse).TotalMinutes;

其他提示

Based on your last comment, try:

MessageBox.Show(result.TotalMinutes.ToString())
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top