更改时间后,我的日期会自动增加。 [英] My date automatically gets incremented when change time.

查看:78
本文介绍了更改时间后,我的日期会自动增加。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我在编辑项目模板中使用文本框来编辑时间。下面是aspx代码。



< asp:TemplateField HeaderText =结束时间>

< ItemTemplate>

< asp:标签ID =lblendtimerunat =serverText ='<%#Bind(Endtime ,{0:u})%>'>< / asp:标签>

< / ItemTemplate>

< EditItemTemplate>

< asp:TextBox ID =txtendtime123runat =serverText ='<%#Bind(Endtime,{0:u})%>'>< / asp:TextBox>

< / EditItemTemplate>

< / asp:TemplateField>



我必须根据计算结束时间计算小时数 - 开始时间工作正常,但问题是我的日期会自动增加,即使我不编辑它。

以下是同样的情景。 />
示例。

我的文本框包含::2013-15-08 23:30:00zz

之后编辑::201 3-15-08 23:56:00zz

更新后::2013-16-08 9:40:00zz



以下是我的C#代码。

i have a gridview for which i use a textbox in edit item template for editing time.Below is aspx code.

<asp:TemplateField HeaderText="EndTime">
<ItemTemplate>
<asp:Label ID="lblendtime" runat="server" Text='<%# Bind("Endtime","{0:u}") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtendtime123" runat="server" Text= '<%# Bind("Endtime","{0:u}") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

I have to calculate number of hours based on calculations end time- start time which works fine but the problem is my date automatically gets incremented even when i dont edit it.
Below is the scenario for same.
Example.
my textbox contains::2013-15-08 23:30:00zz
After editing::2013-15-08 23:56:00zz
After Update::2013-16-08 9:40:00zz

below is my C# code for the same.

TextBox txtdatetimeend = (TextBox)gvr.FindControl("txtendtime123");
string endtime = txtdatetimeend.Text;

string[] arrendtime = endtime.Split(' ');
string timepart = arrendtime[1];

string[] splitendtime = timepart.Split(':');

int endhours = Convert.ToInt32(splitendtime[0]);
int endminutes = Convert.ToInt32(splitendtime[1]);


Label lblstarttime = (Label)gvr.FindControl("lblstarttime");
string starttime = lblstarttime.Text;

string[] arrstarttime = starttime.Split(' ');
string startpart = arrstarttime[1];

string[] splitstart = startpart.Split(':');

int starthours = Convert.ToInt32(splitstart[0]);
int startmin = Convert.ToInt32(splitstart[1]);


double finalhours =Convert.ToDouble(endhours - starthours);

int finalminutes = endminutes - startmin;

double newfinalm = Convert.ToDouble(finalminutes);

double newfinalminutes = newfinalm / 60;

double finalentryhours = finalhours + Convert.ToDouble(newfinalminutes);







DateTime dtfrthus = Convert.ToDateTime(endtime);

        string dtentertime = Convert.ToString(dtfrthus);

        DateTime final = Convert.ToDateTime(dtentertime);

        //var sqlFormattedDate = dtfrthus.Date.ToString("dd/MM/yyyy");

        string querystring = Request.QueryString["Userid"];
        connfordata.Open();
        SqlCommand cmdfrmonday = new SqlCommand("update s001_Timesheets set s001_hrsday4 = '" + finalentryhours + "',s001_TimeEntryEnd = '" + final + "',s001_tsnarrationtxt = '" + narra + "' where s001_TimesheetsID = '" + id + "' ", connfordata);
        cmdfrmonday.ExecuteNonQuery();
        connfordata.Close();

推荐答案

您使用非常非常错误的方法。要查找一些持续时间,请使用 System.DateTime 本身,而不是提取分钟或小时。对于这种类型,定义了减法运算符:

You are using very, very wrong approach. To find some duration, use System.DateTime itself, not extracting minutes or hours. For this type, subtraction operator is defined:
System.DateTime before = //...
System.DateTime after = //...
System.TimeSpan duration = after - before; // that's it
double totalHours = duration.TotalHours;
double totalSeconds = duration.TotalSeconds;
//...





请参阅:

http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / system.timespan%28v = vs.110%29.aspx [ ^ ]。



此外,请参阅我的评论问题。



-SA


这篇关于更改时间后,我的日期会自动增加。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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