Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 3 time string as follows
C#
 string s1="9"; 
 string s2= "82";
string s3= "24";

I want to add string currtime= s1+s2+s3;
but here s2 is more than 60 ,means minute is more than 60 then automatically add one hour.
I want time is like follow
C#
string finalTime="10:22:24 AM"

For the same we use TIME function in excel. "=TIME(A1,B1,B3)"
how can we do this in c#.
Posted
Updated 18-Jan-16 20:50pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Jan-16 1:16am    
It all makes no sense. Working with strings representing data instead of data itself.
Even if you formulate it all properly, it will look quite trivial. What have you tried so far?
—SA
Member 12036327 19-Jan-16 1:38am    
I had do silly coding, to check if minutes more than 60 then minus it from 60 then then add one our in hour. it works fine but I am looking for direct and easy way, If it is there any.

The simple flow will like this.first add H M ans S as individually.then first divide second with 60 and add answer into minute and take remainder as your remain minute then divide minute with 60 and add answer into hour and take remainder as minute remain.
finally you get All H M ans S as answer.
NOTE : do all operation in integer format
 
Share this answer
 
v2
There are basically two ways:
  1. Do yourself the computation
  2. Use TimeSpan[^] objects to perform the computation


In order to use both methods you have to:
  • parse your strings into integers
  • compute addition
  • format your result into a string


See Int32.Parse Method (System)[^], Int32.ToString Method (String) (System)[^].
 
Share this answer
 
C#
 string s1 = "9";
            string s2 = "82";
            string s3 = "24";

            long seconds = Convert.ToInt64(s3) + Convert.ToInt64(s2) * 60+Convert.ToInt64(s1)*3600;
            DateTime startDateTime = DateTime.Today;
            DateTime date = startDateTime.AddSeconds(seconds);
Console.WriteLine(date.ToString("hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture));
            Console.ReadKey();
 
Share this answer
 
v2

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