时钟总和分钟与时间跨度 [英] Clock sum minutes with timespan

查看:84
本文介绍了时钟总和分钟与时间跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,



txt1.text =07:00'时钟

txt2.text =10 '分钟



i希望结果是07:10我想要使用时间跨度。



希望你能帮助我

解决方案

解析两个:

 DateTime dt; 
int 分钟;
if (DateTime.TryParse(txt1.Text, out dt)&&& span class =code-keyword> int .TryParse(txt2.Text, out 分钟))
{
TimeSpan ts = new TimeSpan( 0 ,分钟, 0 );
dt = dt + ts;
Console.WriteLine(dt.ToString( hh:mm));
}


我会执行以下操作:

- 拆分你的txt1.text(在:处)并将2部分转换为整数

- 将其转换为日期

- 将txt2.text转换为整数

- 将此值添加为分钟日期 - 生成之前(myDate.addminutes(cint(txt2.text))


轮到我^ _ ^



< pre lang =c#> CultureInfo provider = CultureInfo.InvariantCulture;
string clockFormat = hh:mm;

string clockStr = txt1.Text;
string minutesStr = txt2.Text;

double 分钟;
DateTime时钟;
TimeSpan时间;

if (!DateTime.TryParseExact(clockStr,clockFormat,provider,DateTimeStyles.None) , o ut clock))
clock = new DateTime( 0 0 0 );

// DateTime很好,除非你在23:40加50分钟。获得额外的日子并不容易。我不知道你是否需要,但是:
TimeSpan clockTime = clock.TimeOfDay;

if double .TryParse(minutesStr, out 分钟))
分钟= 0 0 ;

time = TimeSpan.FromMinutes(分钟);


// 这是数据时间方面。您可以使用TimeOfDay从午夜开始获取TimeSpan:
DateTime totalTime = clock.Add(time);

// 或者你可以使用clockTime获得整个TimeSpan,包括任何额外的天数
TimeSpan totalTimeSpan = clockTime.Add(time);


I have two textbox,

txt1.text = "07:00" 'clock
txt2.text ="10" ' minute

i hope result to be 07:10 i want use timespan.

hope you help me

解决方案

Parse the two:

DateTime dt;
int minutes;
if (DateTime.TryParse(txt1.Text, out dt) && int.TryParse(txt2.Text, out minutes))
   {
   TimeSpan ts = new TimeSpan(0, minutes, 0);
   dt = dt + ts;
   Console.WriteLine(dt.ToString("hh:mm"));
   }


I would do the following :
- split your txt1.text (at the ":") and cast the 2 parts into Integer
- convert it into Date
- cast txt2.text into integer
- add this value as minutes to the Date - generated before (myDate.addminutes(cint(txt2.text))


My turn ^_^

CultureInfo provider = CultureInfo.InvariantCulture;
string clockFormat = "hh:mm";

string clockStr = txt1.Text;
string minutesStr = txt2.Text;

double minutes;
DateTime clock;
TimeSpan time;

if(!DateTime.TryParseExact(clockStr,clockFormat,provider,DateTimeStyles.None, out clock))
    clock = new DateTime(0,0,0);

//DateTime is fine unless you add 50 minutes to 23:40.  It is not as easy to get the extra days.  I don't know if you need to or not, but:
TimeSpan clockTime = clock.TimeOfDay;

if (double.TryParse(minutesStr, out minutes))
    minutes = 0.0;

time = TimeSpan.FromMinutes(minutes);


//This is the datatime aspect.  You can use TimeOfDay to get the TimeSpan since midnight:
DateTime totalTime = clock.Add(time);

//Or you can get the whole TimeSpan including any extra days using the clockTime
TimeSpan totalTimeSpan = clockTime.Add(time);


这篇关于时钟总和分钟与时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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