Click here to Skip to main content
15,913,140 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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();
Posted
Comments
Sergey Alexandrovich Kryukov 4-Dec-13 3:11am    
Looks like a huge abuse. Why would you ever get time from some text?
—SA

1 solution

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:
C#
System.DateTime before = //...
System.DateTime after = //...
System.TimeSpan duration = after - before; // that's it
double totalHours = duration.TotalHours;
double totalSeconds = duration.TotalSeconds;
//...


Please see:
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[^].

Besides, please see my comment to the question.

—SA
 
Share this answer
 
Comments
Amit Karyekar 4-Dec-13 3:46am    
this approach works out perfectly but the thing is,it automatically increments the after date which is out of my understanding.Can you help out for that
Sergey Alexandrovich Kryukov 4-Dec-13 9:13am    
Nothing "increments". If you have to fixed points in time, on the same day or not, it gives fixed time interval between then, correctly calculated. You did not try, or your observations are wrong.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900