C#:如何将当前时间放入字符串中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/414733/
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
C#: How would I get the current time into a string?
提问by
How could I get the current h/m/s AM time into a string? And maybe also the date in numeric form (01/02/09) into another one?
如何将当前的 h/m/s AM 时间转换为字符串?也许也可以将数字形式的日期 (01/02/09) 转换为另一个日期?
回答by Tom Ritter
DateTime.Now.ToString("h:mm tt") DateTime.Now.ToString("MM/dd/yyyy")
DateTime.Now.ToString("h:mm tt") DateTime.Now.ToString("MM/dd/yyyy")
Here are some common format strings
下面是一些常见的格式字符串
回答by Joel Coehoorn
string t = DateTime.Now.ToString("h/m/s tt");
string t2 = DateTime.Now.ToString("hh:mm:ss tt");
string d = DateTime.Now.ToString("MM/dd/yy");
回答by palehorse
You can use format strings as well.
您也可以使用格式字符串。
string time = DateTime.Now.ToString("hh:mm:ss"); // includes leading zeros
string date = DateTime.Now.ToString("dd/MM/yy"); // includes leading zeros
or some shortcuts if the format works for you
或一些快捷方式(如果格式适合您)
string time = DateTime.Now.ToShortTimeString();
string date = DateTime.Now.ToShortDateString();
Either should work.
要么应该工作。
回答by P Daddy
I'd just like to point out something in these answers. In a date/time format string, '/' will be replaced with whatever the user's date separator is, and ':' will be replaced with whatever the user's time separator is. That is, if I've defined my date separator to be '.' (in the Regional and Language Options control panel applet, "intl.cpl"), and my time separator to be '?' (just pretend I'm crazy like that), then
我只想在这些答案中指出一些内容。在日期/时间格式字符串中,'/' 将被替换为任何用户的日期分隔符,而 ':' 将被替换为用户的任何时间分隔符。也就是说,如果我已将日期分隔符定义为 '.' (在区域和语言选项控制面板小程序中,“intl.cpl”),我的时间分隔符是“?” (就假装我像那样疯了),然后
DateTime.Now.ToString("MM/dd/yyyy h:mm tt")
would return
会回来
01.05.2009 6?01 PM
In most cases, this is what you want, because you want to respect the user's settings. If, however, you requirethe format be something specific (say, if it's going to parsed back out by somebody else down the wire), then you need to escape these special characters:
在大多数情况下,这就是您想要的,因为您希望尊重用户的设置。但是,如果您要求格式是特定的(例如,如果它要被其他人解析出来),那么您需要转义这些特殊字符:
DateTime.Now.ToString("MM\/dd\/yyyy h\:mm tt")
or
或者
DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt")
which would now return
现在将返回
01/05/2009 6:01 PM
EDIT:
编辑:
Then again, if you reallywant to respect the user's settings, you should use one of the standard date/time format strings, so that you respect not only the user's choices of separators, but also the general format of the date and/or time.
再说一次,如果你真的想尊重用户的设置,你应该使用标准的日期/时间格式字符串之一,这样你不仅尊重用户对分隔符的选择,而且尊重日期和/或时间的一般格式.
DateTime.Now.ToShortDateString()
DateTime.Now.ToString("d")
Both would return "1/5/2009" using standard US options, or "05/01/2009" using standard UK options, for instance.
例如,两者都将使用标准美国选项返回“1/5/2009”,或使用标准英国选项返回“05/01/2009”。
DateTime.Now.ToLongDateString()
DateTime.Now.ToString("D")
Both would return "Monday, January 05, 2009" in US locale, or "05 January 2009" in UK.
在美国语言环境中,两者都将返回“Monday, January 05, 2009”,或者在英国语言环境中返回“05 January 2009”。
DateTime.Now.ToShortTimeString()
DateTime.Now.ToString("t");
"6:01 PM" in US, "18:01" in UK.
美国的“6:01 PM”,英国的“18:01”。
DateTime.Now.ToLongTimeString()
DateTime.Now.ToString("T");
"6:01:04 PM" in US, "18:01:04" in UK.
美国为“下午 6:01:04”,英国为“18:01:04”。
DateTime.Now.ToString()
DateTime.Now.ToString("G");
"1/5/2009 6:01:04 PM" in US, "05/01/2009 18:01:04" in UK.
“1/5/2009 6:01:04 PM”在美国,“05/01/2009 18:01:04”在英国。
Many other options are available. See docs for standard date and time format stringsand custom date and time format strings.
许多其他选项可用。有关标准日期和时间格式字符串以及自定义日期和时间格式字符串,请参阅文档。
回答by Mike Scott
Be careful when accessing DateTime.Now twice, as it's possible for the calls to straddle midnight and you'll get wacky results on rare occasions and be left scratching your head.
两次访问 DateTime.Now 时要小心,因为调用可能会跨越午夜,并且在极少数情况下您会得到古怪的结果并且让您摸不着头脑。
To be safe, you should assign DateTime.Now to a local variable first if you're going to use it more than once:
为安全起见,如果您要多次使用它,您应该首先将 DateTime.Now 分配给局部变量:
var now = DateTime.Now;
var time = now.ToString("hh:mm:ss tt");
var date = now.ToString("MM/dd/yy");
Note the use of lower case "hh" do display hours from 00-11 even in the afternoon, and "tt" to show AM/PM, as the question requested. If you want 24 hour clock 00-23, use "HH".
请注意,即使在下午,使用小写的“hh”也会显示从 00 到 11 的小时数,并按照问题要求使用“tt”来显示 AM/PM。如果您想要 24 小时制 00-23,请使用“HH”。
回答by Kartiikeya
Method to get system Date and time in a single string
在单个字符串中获取系统日期和时间的方法
public static string GetTimeDate()
{
string DateTime = System.DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
return DateTime;
}
sample OUTPUT :-16-03-2015 07:45:15