C# 你如何获得当前的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:07:34  来源:igfitidea点击:

How do you get the current time of day?

c#datetime

提问by

How do you get the current time (not date AND time)?

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

Example: 5:42:12 PM

示例:下午 5:42:12

回答by Mark Brackett

DateTime.Now.TimeOfDaygives it to you as a TimeSpan(from midnight).

DateTime.Now.TimeOfDay把它作为TimeSpan(从午夜)给你。

DateTime.Now.ToString("h:mm:ss tt")gives it to you as a string.

DateTime.Now.ToString("h:mm:ss tt")将其作为字符串提供给您。

DateTime reference: https://msdn.microsoft.com/en-us/library/system.datetime

日期时间参考:https: //msdn.microsoft.com/en-us/library/system.datetime

回答by Greg D

Get the current date and time, then just use the time portion of it. Look at the possibilities for formatting a date time string in the MSDN docs.

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

回答by Stefan Schultze

Datetime.TimeOfDayreturns a TimeSpanand might be what you are looking for.

Datetime.TimeOfDay返回 aTimeSpan并且可能是您要查找的内容。

回答by benPearce

DateTime.Now.TimeOfDay

or

或者

DateTime.Now.ToShortTimeString()

回答by Oppositional

Current time with AM/PM designator:

带有 AM/PM 指示符的当前时间:

DateTime.Now.ToString("hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("hh:mm:ss.fff tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)

Current time using 0-23 hour notation:

使用 0-23 小时表示法的当前时间:

DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
DateTime.Now.ToString("HH:mm:ss.fff", System.Globalization.DateTimeFormatInfo.InvariantInfo)

回答by Khb

This will show you only the current time, in 24 hour format:

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

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

Regards
K

问候
K

回答by Chris Fulstow

Another option using String.Format()

使用 String.Format() 的另一种选择

string.Format("{0:HH:mm:ss tt}", DateTime.Now)

回答by Chris Fulstow

Try this one. Its working for me in 3tier Architecture Web Application.

试试这个。它在 3tier Architecture Web Application 中为我工作。

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

Please remember the Single Quotes in the insert Query.

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

For example:

例如:

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() + "')";

The DateTimeinsertion at the end of the line.

DateTime在该行的末端插入。

回答by sadhana

To calculate the current datetime:

计算当前日期时间:

DateTime theDate = DateTime.UtcNow;

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

MessageBox.Show(custom);

回答by Musikero31

Try this:

尝试这个:

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

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

For other formats, you can check this site: C# DateTime Format

对于其他格式,您可以查看此站点:C# DateTime Format