Hi @Analyst_SQL ,
DateTime.ParseExact Method Converts the specified string representation of a date and time to its DateTime equivalent.
But you only want the time, you need to use DateTime.ToShortTimeString
Method:Converts the value of the current DateTime object to its equivalent short time string representation.
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshorttimestring?view=net-6.0
DateTime.ParseExact(date2, "HH:mm:ss", CultureInfo.InvariantCulture).ToShortTimeString();
According to your code, I guess you want to replace the empty value with the current time. I slightly modified your code:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
var date2 = (row.FindControl("txtINTime") as TextBox).Text;
var date3 = (row.FindControl("txtOuttime") as TextBox).Text;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (!string.IsNullOrEmpty(date2))
{
//Cells[4] is modified according to its own column. Start from 0.
row.Cells[4].Text = DateTime.ParseExact(date2, "HH:mm:ss", CultureInfo.InvariantCulture).ToShortTimeString();
}
else
{
row.Cells[4].Text = DateTime.Now.ToShortTimeString();
}
if (!string.IsNullOrEmpty(date3))
{
row.Cells[5].Text = DateTime.ParseExact(date3, "HH:mm:ss", CultureInfo.InvariantCulture).ToShortTimeString();
}
else
{
row.Cells[5].Text = DateTime.Now.ToShortTimeString();
}
}
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.