Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Parse the two:
C#
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"));
   }
 
Share this answer
 
Comments
ulungss 30-Apr-15 4:53am    
thanks for your solutions, its work.. great.
OriginalGriff 30-Apr-15 5:03am    
You're welcome!
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))
 
Share this answer
 
Comments
ulungss 30-Apr-15 5:08am    
i see your code use too worn, and impressed manual. I do no want to fully use the vb language. thanks :)
My turn ^_^

C#
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);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900