I want to compare two time values in the format of HR:MIN:SEC. of the type string in c#?
Following is a code that a did in comparison between dates. Is it possible to use that code for strings also. OR Is there any sol?
protected void Button1_Click(object sender, EventArgs e)
{
int uid = Convert.ToInt32(dlFaculty.SelectedValue);
string starttime = Convert.ToString((Convert.ToInt32(dlHour.SelectedItem.Text)) + ":" + (Convert.ToInt32(dlMinute.SelectedItem.Text)) + " " + dlAMPM.SelectedItem.Text);
string endtime = Convert.ToString((Convert.ToInt32(dlHour1.SelectedItem.Text)) + ":" + (Convert.ToInt32(dlMinute1.SelectedItem.Text)) + " " + dlAMPM1.SelectedItem.Text);
DateTime startdate = Convert.ToDateTime(txtStartDate.Text);
DateTime enddate = Convert.ToDateTime(txtEndDate.Text);
DataTable oTable = new DataTable();
Boolean flag = true;
string SQL = "select startdate,enddate from BatchMaster where usermasterid=" + uid + " AND starttime='" + starttime + "' AND endtime='" + endtime + "'";
oTable = DbHelper.ExecuteTable(DbHelper.CONSTRING, CommandType.Text, SQL, null);
//coursemasterid = 0;
//string s = subjectsid;
if (oTable.Rows.Count > 0)
{
DataRow dr = oTable.Rows[0];
开发者_如何学C if ((Convert.ToDateTime(dr["startdate"]) <= startdate && Convert.ToDateTime(dr["enddate"]) >= startdate) || (Convert.ToDateTime(dr["startdate"]) <= enddate && Convert.ToDateTime(dr["enddate"]) >= enddate))
{
//lblavailability.Visible = true;
flag = false;
// lblavailability.Text = "Not Available.";
}
lblavailability.Visible = true;
if (flag == true)
{
lblavailability.CssClass = "textgreen";
lblavailability.Text = "Available.";
}
else
{
lblavailability.CssClass = "errorbold";
lblavailability.Text = "Not Available.";
}
}
else
{
lblavailability.CssClass = "textgreen";
lblavailability.Visible = true;
lblavailability.Text = "Available.";
}
}
You're essentially talking about the behaviour of
Convert.ToDateTime("23:00:10")
As per MSDN "It completes missing month, day, and year information with the current date.", so if you make sure that your time is always in HH and not hh format (that it uses 24h convention), you should be able to use a similar approach to the one above.
That is if you're sure that your code will not span midnight... :)
精彩评论