任何人都可以帮我把时间计算出来。我需要减去具有时间数据的两列值
col1
是aamhr
col2
是aammin
col3
是aamcutoffhr
col4
是cutoffmin
我想以某种方式将aamhr
和aammin
合并为aamhr:aammin
和aamcutoffhr:aamcutoffmin
,我想减去aamhr:aammin-aamcutoffhr:aamcutoffmin
。
为此,我使用了日期时间变量publishtime
,cutofftime
和pubtime
。
publishtime
是aamhr:aammin
。cutofftime
是aamcutoffhr
和aamcutoffmin
的串联。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;
}
}
答案 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/60
和diffmin=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;
...