如何获得当前时间(而不是日期和时间)?

例如:下午5:42:12


当前回答

这将只显示当前时间,以24小时格式显示:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now.ToLongTimeString().ToString());
        Console.WriteLine(DateTime.Now.ToShortTimeString().ToString());
        Console.ReadLine();
    }
}

问候 K

其他回答

计算当前的datetime:

DateTime theDate = DateTime.UtcNow;

string custom = theDate.ToString("d");

MessageBox.Show(custom);
DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt");

试着用它来满足你的需要

试试这个:

DateTime.Now.ToString("HH:mm:ss tt");

对于其他格式,您可以检查这个网站:c# DateTime格式

获取当前日期和时间,然后只使用它的时间部分。看看MSDN文档中格式化日期时间字符串的可能性。

试试这个。它为我工作在3tier架构Web应用程序。

"'" + DateTime.Now.ToString() + "'"

请记住插入查询中的单引号。

例如:

string Command = @"Insert Into CONFIG_USERS(smallint_empID,smallint_userID,str_username,str_pwd,str_secquestion,str_secanswer,tinyint_roleID,str_phone,str_email,Dt_createdOn,Dt_modifiedOn) values ("
 + u.Employees + ","
 + u.UserID + ",'"
 + u.Username + "','"
 + u.GetPassword() + "','"
 + u.SecQ + "','"
 + u.SecA + "',"
 + u.RoleID + ",'"
 + u.Phone + "','"
 + u.Email + "','"
 + DateTime.Now.ToString() + "','"
 + DateTime.Now.ToString() + "')";

行尾的DateTime插入。