如何将时间跨度转换为十进制 [英] How to convert timespan into decimal

查看:165
本文介绍了如何将时间跨度转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生..

我有两个Dropdown List.Dropdown1用于开始Time和Dropdown2用于结束时间。我必须计算时间。我已经完成了使用timespan.now我想要计算总共时间,所以我怎么能为此编码。

我将分享我的代码以便你理解。



 十进制时间=  0 ; 
DateTime time1 = new DateTime();
DateTime time2 = new DateTime();
time1 = DateTime.Parse(ddl1.SelectedItem.Text); // ex..time1 = DateTime。解析(上午8:00);
time2 = DateTime.Parse(ddl2.SelectedItem.Text); // ex .. time2 = DateTime.Parsee(10:00 AM);
TimeSpan ts = time2.Subtract(time1); // ex .. ts = 10.00.Subtract(8:00)so ts = 2:00
ll1.Text = Convert.ToString(ts); // ex .. ll1.Text = 2:00;
// 这两行我不知道它是对还是错。
time = time + ts; // 错误无法使用+运算符Decimal到System.TimeSpan
Label2.Text = Convert.ToString(时间);





假设..

我有循环所以agian这个pr ocess work。

so first Time ll1.Text = 2:00

Secong Time ll1.Text = 2:30;

现在我想要计算总时间2:00 + 2:30 = 4:30并存入另一个标签。



请Anu身体有这个解决方案的代码plz帮我这个。

解决方案

为什么不将时间变量定义为TimeSpan?

 TimeSpan time = TimeSpan.Zero; 
// ...
time + = ts;
Label2.Text = time.ToString();



可行。

最好使用ToString()方法;尽可能避免转换类。


尝试使用以下代码:

 DateTime time1 =  DateTime(); 
DateTime time2 = new DateTime();

// 第一次下拉选择
time1 = DateTime。解析( 8:00 AM);
time2 = DateTime.Parse( 10:00 AM);
TimeSpan ts = time2.Subtract(time1); // 02:00:00

// 第二次下拉选择
time1 = DateTime.Parse( 8:00 AM);
time2 = DateTime.Parse( 9:30 AM);
TimeSpan ts1 = time2.Subtract(time1); // 01:30:00

TimeSpan tsTotal = ts + ts1;

string t1 = Convert.ToString(tsTotal); // 03:30:00
Label2.Text = t1;


Hello Sir..
I have two Dropdown List.Dropdown1 is for start Time and Dropdown2 is for end time.I have to calculate time.That I have done using timespan.now i want calculate total of time so how can i code for that.
I will share my code so you will understand.

decimal time = 0;
DateTime time1 = new DateTime();
DateTime time2 = new DateTime();
time1 = DateTime.Parse(ddl1.SelectedItem.Text);//ex..time1=DateTime.Parse(8:00AM);
time2 = DateTime.Parse(ddl2.SelectedItem.Text);//ex.. time2=DateTime.Parsee(10:00AM);
TimeSpan ts = time2.Subtract(time1);//ex.. ts= 10.00.Subtract(8:00) so ts=2:00
ll1.Text = Convert.ToString(ts);//ex.. ll1.Text=2:00;
//this two line i dont have idea it is right or wrong.
time = time + ts;//Error Cannot use + operator Decimal to System.TimeSpan
Label2.Text = Convert.ToString(time);



Suppose..
I have Loop so agian this process work.
so first Time ll1.Text=2:00
Secong Time ll1.Text=2:30;
Now I want to calculate total time 2:00+2:30 =4:30 and store into another label .

Please Anu body have code for this solution plz help me for this.

解决方案

Why don't you define your time variable as a TimeSpan?

TimeSpan time = TimeSpan.Zero;
// ...
time += ts;
Label2.Text = time.ToString();


would work.
And better use the ToString() method; Convert class should be avoided when possible.


Try with below code:

DateTime time1 = new DateTime();
DateTime time2 = new DateTime();

// 1st time dropdown selection
time1 = DateTime.Parse("8:00AM");
time2 = DateTime.Parse("10:00AM");
TimeSpan ts = time2.Subtract(time1); // 02:00:00

// 2nd time dropdown selection
time1 = DateTime.Parse("8:00AM");
time2 = DateTime.Parse("9:30AM");
TimeSpan ts1 = time2.Subtract(time1); // 01:30:00

TimeSpan tsTotal = ts + ts1;

string t1 = Convert.ToString(tsTotal); // 03:30:00
Label2.Text = t1;


这篇关于如何将时间跨度转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆