9

私のアプリケーションでは、4 つの TextBox と、開始時刻を入力するための 2 つの TextBox と、終了時刻を入力するための 2 つの TextBox があります。

ユーザーは常に完了時間を入力するため、入力は常に 11:30、12:45 などになります。

開始時刻と終了時刻の時間と分の差を取得するにはどうすればよいですか?

4

8 に答える 8

16

TimeSpan クラスと DateTime の Subtract メソッドを使用します。

        DateTime t1 = Convert.ToDateTime(textBox1.Text);
        DateTime t2 = Convert.ToDateTime(textBox2.Text);
        TimeSpan ts = t1.Subtract(t2);
于 2012-05-08T14:01:14.833 に答える
6

TimeSpan引き算で差額を求めることができます。

TimeSpan time1 = TimeSpan.Parse(textBox1.Text);
TimeSpan time2 = TimeSpan.Parse(textBox2.Text);

TimeSpan difference = time1 - time2;

int hours = difference.Hours;
int minutes = difference.Minutes;
于 2012-05-08T14:02:08.263 に答える
5

create two DateTime objects parsing the values in the TextBox controls and simply subtract the two DateTime, you will get a TimeSpan object which is what you are looking for.

于 2012-05-08T14:00:39.523 に答える
3

TimeSpan を使用し、日付を使用する必要はありません

var start = TimeSpan.Parse(start.Text);
var end = TimeSpan.Parse(end.Text);

TimeSpan result = end - start;
var diffInMinutes = result.TotalMinutes();
于 2012-05-08T14:04:11.260 に答える
1
TimeSpan difference = DateTime.Parse(txtbox1.Text) - Datetime.Parse(txtbox2.Text);

Example:

TimeSpan difference =  DateTime.Parse("12:55")-DateTime.Parse("11:45");
double hourDiff=  difference.TotalHours;
double minutes = difference.TotalMinutes;
Console.WriteLine(hourDiff);//1.16666666666667
Console.WriteLine(minutes);//70
于 2012-05-08T14:00:39.367 に答える
1

タイムスパンを使用:

    DateTime dt1 = new DateTime(starttime.text);
    DateTime dt2 = new DateTime(endtime.text);

    TimeSpan result = dt2 - dt1;

次に、結果から分、秒などを取得できます。

于 2012-05-08T14:02:39.110 に答える
0

convert the hours to minutes, add it to the existing minutes, convert those down to total seconds, do the same with endtime. minus them from each other, convert them back up to hours and minutes. remembering 60 minutes in an hour, 60 seconds in a minute, thats how i would deal with it. because total seconds will always be the same, its hard trying to teach a computer trying to wrap around from 60 back to 0 or from 12 to 1. much easier to use somethign perfectly linear like seconds. then reconvert upwards

于 2012-05-08T14:00:37.377 に答える
0

2 つの を減算して、構造体DateTimeを受け取ることができます。これには、、、、、などをTimeSpan取得するためのプロパティがあります。DaysHoursMinutesSeconds

var first = new DateTime(2012, 05, 08, 10, 30, 00);
var second = new DateTime(2012, 05, 08, 11, 49, 13);

var diff = first - second;
var hours = diff.Hours;
var mins = diff.Minutes;
于 2012-05-08T14:03:49.987 に答える