Calculating days from TimeSpan hours

一笑奈何 提交于 2020-01-04 05:23:07

问题


I have 1 single text box which a user will enter the number of hours. At present, if they enter 26 hours, we get an error because of the TimeSpan's HH limit. This value is going to get stored in a SQL Server 2008 Time(7) field.

How can I get it to recognize more than 23 hours? It is not an option to store it as a decimal because another section of the program requires this field to be a time(7) field.

 TimeSpan estiamtedHours;

 private void btnSave_Click(object sender, EventArgs e)
 {
     estimatedHours = TimeSpan.Parse(tbEstHours.Text);
 }

The time(7) field also has the limit of 24 hours, what would be the best way round this as Time(7) is required for a Stopwatch on another form.

Thanks


回答1:


Be careful. TimeSpan is meant to measure an elapsed duration of time, while time in SQL Server is specifically a time-of-day. These are two different concepts.

Sometimes these get mixed up. For example, DateTime.TimeOfDay is a TimeSpan type - which goes against its design. It's a reasonable compromise since there is no Time type in .Net and it can fit.

But a TimeSpan that is 24 hours or greater will not fit into a SQL Server time field.

Also, a TimeSpan is based on standard days. You can create one with TimeSpan.FromHours(26) and it will represent "1 day and 2 hours". If you call TimeSpan.FromHours(26).ToString() it will be "1.02:00:00".

If you're storing an elapsed duration of time (not a time of day), then use a TimeSpan in .Net, but use an integer type in SQL Server. Decide what units you want precision for, and that will help you choose a data type.

For example, you can store the full precision of TimeSpan.Ticks using a SQL Server bigint type. But probably you will store TimeSpan.TotalSeconds using an int. When loading, you can use TimeSpan.FromSeconds to get back to a TimeSpan type.

Also be aware that a TimeSpan can be negative, which represents moving backwards in time.

By the way, if you used the Noda Time library - these concepts would be separated for you in types called Duration and LocalTime.

If what you were after is a way to parse a string like "26:00:00" you can't do that with a TimeSpan. But you can use Noda Time:

// starting from this string
string s = "26:00:00";

// Parse as a Duration using the Noda Time Pattern API
DurationPattern pattern = DurationPattern.CreateWithInvariantCulture("H:mm:ss");
Duration d = pattern.Parse(s).Value;
Debug.WriteLine(pattern.Format(d));  // 26:00:00

// if you want a TimeSpan, you can still get one.
TimeSpan ts = d.ToTimeSpan();
Debug.WriteLine(ts);  // 1.02:00:00



回答2:


If you know the input value is an hour value as a floating point number, you can use TimeSpan.FromHours():

TimeSpan estiamtedHours;

private void btnSave_Click(object sender, EventArgs e)
{
  estimatedHours = TimeSpan.FromHours(Double.Parse(tbEstHours.Text));
}



回答3:


Parse the text into an int and pass that int as the hours parameter in the TimeSpan constructor.

int hours;
if (Int32.TryParse(tbEstHours.Text, out hours))
{
    TimeSpan ts = new TimeSpan(hours, 0, 0);
}

You can also do the same with minutes and seconds. Alternatively, if you just want hours, you can use TimeSpan.FromHours in the same manner instead of the TimeSpan constructor.




回答4:


After parsing the input, use the FromHours method:

double hours
if (double.TryParse(tbEstHours.Text, out hours)
{
    TimeSpan time = TimeSpan.FromHours(hours);
}



回答5:


The TimeSpan.Parse Method expects the input in the format

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

where hh is the hour part, ranging from 0 to 23.

For example,

  • TimeSpan.Parse("5") returns 5 days,
  • TimeSpan.Parse("5:14") returns 5 hours and 14 minutes.

If you just want your users to enter a number of hours, you can simply parse the input as an integer and construct a TimeSpan from that:

TimeSpan result = TimeSpan.FromHours(int.Parse("26"));
// result == {1.02:00:00}

(Use int.TryParse for user input.)

If you want your users to enter both hours and minutes (such as 26:14), then you need to implement some parsing method yourself.




回答6:


Since the other answers don't address this

The concern here is the time column in the database and it expects a valid duration which would be limited to the 24 hr time where as TimeSpan can have them beyond the 24 hr limit.

So you should ideally parse the value as int (use int.Parse or int.TryParse) and then check if it is less than 24 and then create the appropriate TimeSpan



来源:https://stackoverflow.com/questions/18332256/calculating-days-from-timespan-hours

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!