2类型时间之间的区别 [英] Difference Between 2 field of type time

查看:49
本文介绍了2类型时间之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的表单上有2个文本框。输入输入时间的第一个框和输入输出时间的第二个框。

例如textbox1中的7:30和textbox2中的12:25。



1-在第一个,在SQL Server数据库表中,必须为这两个时间字段选择什么类型?

有一个类型的日期时间,但我想只保存时间而不是日期。



2-如何计算2个时间字段之间的差异?以下命令是错误的:

DateTime dt1 = new DateTime();

dt1 = Convert.ToDateTime(textBox1.Text) - Convert.ToDateTime(textBox1.Text); < br $> b $ b

非常感谢

Hi I have 2 Textbox on my form. First box for enter input time and second for enter output time.
For example 7:30 in textbox1 and 12:25 in textbox2.

1- In the first ,in SQL Server DB table, what type must be select for these 2 time fields?
There is a type datetime but I wants save only time not date.

2- How can I calculate difference between 2 time fields? following commands is wrong:
DateTime dt1 = new DateTime();
dt1 = Convert.ToDateTime(textBox1.Text) - Convert.ToDateTime(textBox1.Text);

Thanks very much

推荐答案

  1. DATE。
System.DateTime earlier = //...
System.DateTime later = //...
System.TimeSpan duration = later - earlier;
// even if earlier > later, by some reason, negative time spans are also acceptable







-SA


HI



1.哪个版本的SQL SERVER可以做你用 ?在SQL SERVER 2008 R2中有时间类型可以存储时间跨度,有商店日期和时间的datetime和datetime



但无论你是否不能使用时间类型您可以将时间存储在日期时间类型字段中,然后当您从数据库中检索此值时,您只需要时间部分并使用



HI

1. which version of SQL SERVER do you use ? in SQL SERVER 2008 R2 there is time type which can store timespan, there is datetime and datetime2 for store date and time

but no matter if you cannot use the time type you can store your time in the datetime type field and then when you retrive this value from your database you take only the time part and work with

Dim objDate As DateTime = CDate( datarow("DateTimeField"))
Dim objTime As String =  objDate.ToShortTimeString()







2.用于计算两个时间字段之间的差异,你可以使用TimeSpan或DateDiff这样的






2. for calculate the difference between two time field you can use TimeSpan or DateDiff like this

Dim timeDiff As Long = DateDiff(DateInterval.Hour, objDate1, objDate2)











or

Dim objDate1 As DateTime = Date.Now()
Threading.Thread.Sleep(3000)
Dim objDate2 As DateTime = Date.Now()
Dim span As TimeSpan = objDate2 - objDate1
Console.WriteLine(span.TotalSeconds)


你可以按照下面的代码来减去时间作为例子:





You can follow the bellow code to subtract time as an example:


DateTime dt = DateTime.Now;
DateTime dt2 = Convert.ToDateTime("1/2/2013 20:30");
TimeSpan ts = (dt2 - dt);
Response.Write(ts.ToString("hh\\:mm\\:ss"));





-Thanks,

Mamun



-Thanks,
Mamun

这篇关于2类型时间之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆