简体   繁体   English

则DateTime.ToString()?

[英]DateTime.ToString()?

I am having a problem. 我遇到了问题。 How do I show the text of my Date correctly? 如何正确显示我的日期文本?

using DateTime.Now I was able to recieve the date & time the computers clock is set to currently, however, when I try to parse into a string format, it also shows the date like: 12-04-2012 12:38 but I was trying to get the time string only, like 12:38 only? 使用DateTime.Now我能够接收计算机时钟设置为当前的日期和时间,但是,当我尝试解析为字符串格式时,它还显示日期:12-04-2012 12:38 我试图只获得时间字符串,只有12:38

What I tried so far was Console.WriteLine(DateTime.Now.ToString("00:00:00")); 我到目前为止尝试的是Console.WriteLine(DateTime.Now.ToString("00:00:00"));

But it did not work =/ 但它不起作用= /

DateTime has a ToShortTimeString method defined: DateTime定义了ToShortTimeString方法:

DateTime.Now.ToShortTimeString()

Or, you can use a custom format string : 或者,您可以使用自定义格式字符串

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

Alternatively, use the standard format string for short time format : 或者,使用标准格式字符串作为短时间格式

DateTime.Now.ToString("t")

One solution that works for a lot of problems is reading documentation : http://msdn.microsoft.com/en-us/library/zdtaw1bw%28v=vs.80%29.aspx 一个适用于许多问题的解决方案是阅读文档: http//msdn.microsoft.com/en-us/library/zdtaw1bw%28v=vs.80%29.aspx

Using Format "t" seems to do what you want 使用格式“t”似乎做你想要的

Just use this: 只要用这个:

DateTime.Now.ToString("hh:mm");

or 要么

DateTime.Now.ToShortTimeString();

For using DateTime.Now.ToString() , this link is very useful and explains all patterns: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm 对于使用DateTime.Now.ToString() ,此链接非常有用,并解释了所有模式: http//www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

Patterns are like following: 模式如下:

DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")

Tuesday, 22 August 2006 06:30:07 2006年8月22日星期二06:30:07

DateTime.Now.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'")

Tue, 22 Aug 2006 06:30:07 GMT 2006年8月22日星期二06:30:07 GMT

DateTime.Now.ToString("MM/dd/yyyy")

08/22/2006 2006年8月22日

Try to format like this: 尝试格式如下:

Console.WriteLine(DateTime.Now.ToString("hh:mm:ss"));

or 要么

Console.WriteLine(DateTime.Now.ToString("hh:mm"));

But my favorite is 但我最喜欢的是

Console.WriteLine(DateTime.Now.ToShortTimeString());

You could try: 你可以尝试:

var dt = DateTime.Now.TimeOfDay; var dt = DateTime.Now.TimeOfDay; dt.ToString().Substring(0, 5); dt.ToString()。子串(0,5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM