有人能帮我算出这里面的时间计算吗。我需要减去具有时间数据的两列值
col1
就是aamhr
col2
就是aammin
col3
就是aamcutoffhr
col4
就是cutoffmin
我想以某种方式将aamhr
和aammin
合并为aamhr:aammin
和aamcutoffhr:aamcutoffmin
,并减去aamhr:aammin-aamcutoffhr:aamcutoffmin
。
为此,我使用了日期时间变量publishtime
、cutofftime
和pubtime
。
- CCD_ 17是CCD_
- CCD_ 19是CCD_ 20和CCD_
- 两者的差异为CCD_ 22
但是我犯了错误。我已经尝试了以下代码:
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;
}
}
您应该使用TimeSpan并假设字段是integer:
...
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;
...
设diff=(aamhr-aamcutoffhr)*60+aammin-aamcutoffmin
。然后是CCD_ 24和CCD_。
在两个日期时间(字面意思是:date1-date2(上使用减法运算符(-(会产生TimeSpan类型的对象,其中包含计算日期时间差所需的所有内容。
你说你犯了很多错误,你能更具体一点吗?
签出时间跨度
它可以帮助您计算两次之间的差异
要合并2列,可以这样使用:
DateTime.ParseExact(DateTime.Today() + HourColumn + ":" + MinuteColumn, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture)
示例:
string startTime = "7:00 AM";
string endTime = "2:00 PM";
TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
Console.WriteLine(duration);`