How do I format a timespan to show me total hours?
Asked Answered
A

2

25

I want to save the user's hours worked in a database varchar column, but by default, the formatted value includes days if the number of hours is more than 24. I just want the total number of hours.

For example: if a user works 10:00:00 hours today, then 13:00:00 hours tomorrow, and 3:30:00 hours the day after tomorrow then the formatted total I want is 26:30:00. Instead, I am seeing 1.2:30:00.

How can I get the formatting I want?

Also, when I save the value 40:00:00 in the database manually, and try to read it into a TimeSpan later, I get a bug.

How can I save the hours in the database the way I want, and still be able to read it back into a TimeSpan later?

Aubigny answered 20/5, 2011 at 15:45 Comment(3)
Are you asking about data in the database or the TimeSpan structure in the BCL? Your question is confusing.Tenuto
As suggested here, it's best to store ticks in the database, rather than a formatted TimeSpan. Then you don't have this problem.Pharsalus
Vote on the feature suggestion, then wait a decade for MS to get around to it: visualstudio.uservoice.com/forums/121579-visual-studio-2015/…Amara
F
24

Try TimeSpan.TotalHours

String timeStamp = "40:00:00";
var segments = timeStamp.Split(':');

TimeSpan t = new TimeSpan(0, Convert.ToInt32(segments[0]), 
               Convert.ToInt32(segments[1]), Convert.ToInt32(segments[2]));
string time = string.Format("{0}:{1}:{2}", 
           ((int) t.TotalHours), t.Minutes, t.Seconds);
Fabiolafabiolas answered 20/5, 2011 at 15:47 Comment(16)
@Bala it get something so...(40.166666663) (it is a example) and how do i convert this 40:00:00 to timespan? i tried timespan x=timespan.Parse("40:00:00"); it get a bugAubigny
@Aubigny TimeSpan.FromHours(40.0);Fabiolafabiolas
@Aubigny for the 40.166666 issue, you can use TimeSpan.TotalHours as integet and then TimeSpan.Minutes and TimeSpan.Seconds to format the string the way you want it.Fabiolafabiolas
@angel, see my edit, you can use FromHours() to convert 40 hoursFabiolafabiolas
@Bala how do i convert "40:00:00" to double then? because it is saved in database ...Aubigny
@Aubigny you can use split on your input string and extract different segments as shown above.Fabiolafabiolas
i plus it one hour more and i get 41.00:00:00 what is this value? after first point?Aubigny
I don't understand. Can you rephrase your question? are you trying to add one hour to a timespan ?Fabiolafabiolas
i want to be able than to have a time and plus with another time (one or two or more ) hoursAubigny
You can try timeSpan.Add(anotherTimeSpan);Fabiolafabiolas
and finally what is it going to return? i want it returns hours, minutes, seconds... is it going to return it?Aubigny
Add() returns a new TimeSpan instance. you can do newTimeSpan.TotalHours to get hours, newTimeSpan.Minutes and newTimeSpan.Seconds to get minutes and seconds.Fabiolafabiolas
almost! the problem now is totalHours get if it is 40:20:00 i got 40,3333 and with minutes and seconds i got finnaly 40,333333333:20:00Aubigny
(int) before than time.TotalHours+":+time.Minutes.ToString()+":"+time.Seconds all this as string and i printed it, do you believe i dont get bugs with this?Aubigny
it should work but rather than concatenating string like that, use string.Format() which is more readable and efficient but your statement should work too.Fabiolafabiolas
Noyce! (So much easier than built in crap)Aimo
E
31

You could do something like:

TimeSpan time = ...;
string timeForDisplay = (int)time.TotalHours + time.ToString(@"\:mm\:ss");
Emergency answered 20/5, 2011 at 15:55 Comment(2)
seriously? ... TimeSpan time = TimeSpan.FromHours(40) + TimeSpan.FromMinutes(30)Emergency
Or even easier: TimeSpan time = new TimeSpan(40, 30, 0) msdn.microsoft.com/en-us/library/bk8a3558.aspxEmergency
F
24

Try TimeSpan.TotalHours

String timeStamp = "40:00:00";
var segments = timeStamp.Split(':');

TimeSpan t = new TimeSpan(0, Convert.ToInt32(segments[0]), 
               Convert.ToInt32(segments[1]), Convert.ToInt32(segments[2]));
string time = string.Format("{0}:{1}:{2}", 
           ((int) t.TotalHours), t.Minutes, t.Seconds);
Fabiolafabiolas answered 20/5, 2011 at 15:47 Comment(16)
@Bala it get something so...(40.166666663) (it is a example) and how do i convert this 40:00:00 to timespan? i tried timespan x=timespan.Parse("40:00:00"); it get a bugAubigny
@Aubigny TimeSpan.FromHours(40.0);Fabiolafabiolas
@Aubigny for the 40.166666 issue, you can use TimeSpan.TotalHours as integet and then TimeSpan.Minutes and TimeSpan.Seconds to format the string the way you want it.Fabiolafabiolas
@angel, see my edit, you can use FromHours() to convert 40 hoursFabiolafabiolas
@Bala how do i convert "40:00:00" to double then? because it is saved in database ...Aubigny
@Aubigny you can use split on your input string and extract different segments as shown above.Fabiolafabiolas
i plus it one hour more and i get 41.00:00:00 what is this value? after first point?Aubigny
I don't understand. Can you rephrase your question? are you trying to add one hour to a timespan ?Fabiolafabiolas
i want to be able than to have a time and plus with another time (one or two or more ) hoursAubigny
You can try timeSpan.Add(anotherTimeSpan);Fabiolafabiolas
and finally what is it going to return? i want it returns hours, minutes, seconds... is it going to return it?Aubigny
Add() returns a new TimeSpan instance. you can do newTimeSpan.TotalHours to get hours, newTimeSpan.Minutes and newTimeSpan.Seconds to get minutes and seconds.Fabiolafabiolas
almost! the problem now is totalHours get if it is 40:20:00 i got 40,3333 and with minutes and seconds i got finnaly 40,333333333:20:00Aubigny
(int) before than time.TotalHours+":+time.Minutes.ToString()+":"+time.Seconds all this as string and i printed it, do you believe i dont get bugs with this?Aubigny
it should work but rather than concatenating string like that, use string.Format() which is more readable and efficient but your statement should work too.Fabiolafabiolas
Noyce! (So much easier than built in crap)Aimo

© 2022 - 2024 — McMap. All rights reserved.