如何计算两次之间的差异

时间:2015-06-08 10:26:52

标签: c# sql-server datetime

任何人都可以帮我把时间计算出来。我需要减去具有时间数据的两列值

  • col1aamhr
  • col2aammin
  • col3aamcutoffhr
  • col4cutoffmin

我想以某种方式将aamhraammin合并为aamhr:aamminaamcutoffhr:aamcutoffmin,我想减去aamhr:aammin-aamcutoffhr:aamcutoffmin

为此,我使用了日期时间变量publishtimecutofftimepubtime

  • publishtimeaamhr:aammin
  • 的串联
  • cutofftimeaamcutoffhraamcutoffmin的串联。
  • 两者的区别是pubtime

但我收到错误。我尝试了以下代码:

public Boolean CheckAamTime(string day)
{
    //DRM.Connectdb  obj= new DRM.Connectdb();

    string qry;
    Boolean flag=false;
    DateTime publishtime;
    DateTime cutofftime,pubtime;
    SqlConnection SqlCon= null;
    SqlCon = new SqlConnection(SqlConStr);
    //      
    qry="select jas_L_hour, jas_L_minute,jas_L_cutoffhour,jas_L_cutoffminute  from journalaopschedule where" 
        & "jas_L_journalid in " & "(" & "select jm_L_journalid from journalmaster where jm_T_acronym='jid') and JAS_L_Day"& "='monday' and " & "JAS_IsAAM"& "='1'";
    SqlCon.Open();  
    SqlCommand cmd= new SqlCommand(qry,SqlCon);
    SqlDataReader SqlRead = cmd.ExecuteReader();    
    if(SqlRead.HasRows)
    {
        while(SqlRead.Read())
        {
            publishtime=Convert.ToDateTime(SqlRead["jas_L_hour"] );
            publishtime= publishtime & ":" &  Convert.ToDateTime(SqlRead["jas_L_minute"]);
            cutofftime=Convert.ToDateTime(SqlRead["jas_L_cutoffhour"].ToString());
            cutofftime= cutofftime & ":" & Convert.ToDateTime (SqlRead["jas_L_cutoffminute"].ToString());
        }
    }
    else
    {
        // sendemail menuscript time has not set
    }

    SqlCon.Close();
    pubtime= publishtime.Subtract(cutofftime);
    //log("timediff" & pubtime.ToString );

    if (pubtime>=DateTime.Today)
    {
        flag=true;
    }
}

4 个答案:

答案 0 :(得分:0)

结帐TimeSpan

它可以帮助你计算2次之间的差异

要合并2个列,您可以像这样使用:

<TextBlock Text="{Binding ButtonValue, Mode=OneWay}"/>

示例:

DateTime.ParseExact(DateTime.Today() + HourColumn + ":" + MinuteColumn, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)

答案 1 :(得分:0)

diff=(aamhr-aamcutoffhr)*60+aammin-aamcutoffmin。然后diffhr=diff/60diffmin=diff%60

答案 2 :(得分:0)

在两个日期时间(字面意思:date1 - date2)上使用减法运算符( - )会生成TimeSpan类型的对象,其中包含计算日期时间差异所需的全部内容。

你说过你收到很多错误,你能说得更具体吗?

答案 3 :(得分:0)

您应该使用TimeSpan并假设字段是整数:

...
TimeSpan publishtime;
TimeSpan cutofftime, pubtime;
...
if (SqlRead.HasRows)
{
    while (SqlRead.Read())
    {
        publishtime = new TimeSpan(Convert.ToInt32(SqlRead["jas_L_hour"]), Convert.ToInt32(SqlRead["jas_L_minute"]), 0);
        cutofftime = new TimeSpan(Convert.ToInt32(SqlRead["jas_L_cutoffhour"]), Convert.ToInt32(SqlRead["jas_L_cutoffminute"]), 0);
    }
}
...
pubtime = publishtime - cutofftime;
...