从 TimeSpan 小时计算天数 [英] Calculating days from TimeSpan hours
问题描述
我有 1 个文本框,用户将在其中输入小时数.目前,如果他们输入 26 小时,我们会因为 TimeSpan 的 HH 限制而得到一个错误.该值将存储在 SQL Server 2008 Time(7) 字段中.
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.
我怎样才能让它识别超过 23 小时?不能将其存储为十进制数,因为程序的另一部分要求此字段为 time(7) 字段.
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);
}
time(7) 字段也有 24 小时的限制,最好的方法是解决这个问题,因为另一个表单上的秒表需要 Time(7).
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.
谢谢
推荐答案
小心.TimeSpan
用于测量经过的持续时间,而 SQL Server 中的 time
专门表示一天中的时间.这是两个不同的概念.
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.
有时这些会混淆.例如,DateTime.TimeOfDay
是一种 TimeSpan
类型 - 这与其设计背道而驰.这是一个合理的妥协,因为 .Net 中没有 Time
类型,它可以适合.
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.
但是TimeSpan
为 24 小时或更长将不适合 SQL Server time
字段.
But a TimeSpan
that is 24 hours or greater will not fit into a SQL Server time
field.
此外,TimeSpan
基于标准天数.您可以使用 TimeSpan.FromHours(26)
创建一个,它将代表1 天 2 小时".如果您调用 TimeSpan.FromHours(26).ToString()
,它将是 "1.02:00:00"
.
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"
.
如果您要存储经过的持续时间(不是一天中的某个时间),则在 .Net 中使用 TimeSpan
,但在 SQL Server 中使用整数类型.确定您想要的精度单位,这将有助于您选择数据类型.
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.
例如,您可以使用 SQL Server bigint
类型存储 TimeSpan.Ticks
的完整精度.但您可能会使用 int
存储 TimeSpan.TotalSeconds
.加载时,您可以使用 TimeSpan.FromSeconds
回到 TimeSpan
类型.
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.
另请注意,TimeSpan
可以为 负,表示时间向后移动.
Also be aware that a TimeSpan
can be negative, which represents moving backwards in time.
顺便说一下,如果您使用了 Noda Time 库 - 这些概念将在称为 Duration
和 LocalTime
.
By the way, if you used the Noda Time library - these concepts would be separated for you in types called Duration
and LocalTime
.
如果你想要的是一种解析像 "26:00:00"
这样的字符串的方法,你不能用 TimeSpan代码>.但是你可以使用Noda Time:
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
这篇关于从 TimeSpan 小时计算天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!