如何计算没有一天 [英] How to calculate no of day

查看:85
本文介绍了如何计算没有一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<tr>
<td valign="top" align="center">Training Date</td>
<td valign="top" align="left"><b>FROM</b>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtDatefrom" runat="server" Width="240px" Height="30px"></asp:TextBox>

    <asp:Image ID="imgDatefrom" runat="server" ImageUrl="~/img/cal.gif" />
     <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server"

        TargetControlID="txtDatefrom" WatermarkText="1/1/2001">
    </cc1:TextBoxWatermarkExtender>
    <cc1:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="imgDatefrom" TargetControlID="txtDatefrom" PopupPosition="TopRight"  >
    </cc1:CalendarExtender>
</td>
<td valign="top" colspan="5" align="left"><b>TO</b>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtDateto" runat="server" Width="300px" Height="30px"></asp:TextBox>
    <asp:Image ID="imgDateto" runat="server" ImageUrl="~/img/cal.gif" />
    <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender2" runat="server"

        TargetControlID="txtDateto" WatermarkText="1/1/2001">
    </cc1:TextBoxWatermarkExtender>
    <cc1:CalendarExtender ID="CalendarExtender2" runat="server" PopupButtonID="imgDateto" TargetControlID="txtDateto" PopupPosition="TopRight" >
    </cc1:CalendarExtender>
</td>
</tr>

<tr>
<td valign="top" align="center">No of Days(in numeric)</td>
<td valign="top" colspan="6" align="left">
    <asp:TextBox ID="txtNoofdays" runat="server" Width="300px" Height="30px"></asp:TextBox>
</tr>







in .CS






in .CS

public void InsertData()
      {
          string fromdate = System.DateTime.Now.ToString();
          txtDatefrom.Text = Convert.ToDateTime(fromdate).ToString();
          string todate = System.DateTime.Now.ToString();
          txtDateto.Text = Convert.ToDateTime(todate).ToString();
          string noofday = 

da = new SqlDataAdapter("Insert Into Feedback_training(From_training_date,To_training_date,Noof_days)values('" + txtDatefrom.Text + "','" + txtDateto.Text + "','"+noofday+"')", con);
            ds = new DataSet();
            da.Fill(ds);



注意: - 我已经插入了fromdate和todate的值。我想要天数记录并存储在数据库中(如何从日期和todate中区分b / w)我已经在数据库中有一天没有天数,数据类型是int

推荐答案

嗨试试这个..



将为你提供差异整数整数。

Hi Try this..

.Days will gives you the diff in integer whole number.
DateTime dtFrom = DateTime.Now.AddDays(-5);
            DateTime dtTo = DateTime.Now; 
            int NoOfDays = (dtTo - dtFrom).Days;









TotalDays 将为您提供 double 包括时间





TotalDays will gives you result in double including the time hours

DateTime dtFrom = DateTime.Now.AddDays(-5);
            DateTime dtTo = DateTime.Now.AddHours(6);
            double  NoOfDays = (dtTo - dtFrom).TotalDays;


参见代码< br $>


string noofday =(System.DateTime.Now - Convert.ToDateTime(fromdate))。 TotalDays .ToString();



请注意,我使用TotalDays属性来获取介于两者之间的天数。这让我考虑了分数天数(366天的年数)的天数。考虑到所有年份仅包含365天,我还可以使用属性Days来获得价值。
See Code

string noofday = (System.DateTime.Now - Convert.ToDateTime(fromdate)).TotalDays.ToString();

Note that I used the TotalDays property to get the number of days in between. This gets me the number of days putting in consideration the years with fraction days (years with 366 days). I could also use the property "Days" that would get me the value considering that all the years consist of 365 days only.


首先,请不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。



直接传递DateTime值,直接作为数字传递天数 - 不要转换为字符串,因为这只会在SQL时引入错误服务器的配置与Web服务器的配置不同。



计算天数很简单:

First off, don't do that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Pass the DateTime values directly, and the number of days directly as numbers - don't play around with converting the to strings, because that just introduces errors when you SQL server is not configured the same as the Web server.

To Calculate the number of days is simple:
int noOfDays = (todate - fromdate).Days;


这篇关于如何计算没有一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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