是否可以在打印日期时间列表时仅显示日期?

时间:2014-12-02 11:56:09

标签: c# datetime

this.reportTextBox.Text += String.Join(Environment.NewLine, notEqualValue);

此代码行将打印出此列表中的所有元组值。它将显示如下:

  

(02/12/14 00:00:00,你好)

我只希望显示DateTime方法的Date函数。所以看起来应该是这样的:

  

(02/12/14,你好)

这可能吗?

元组的初始化可以在下面看到。

 Tuple<DateTime, string> a = new Tuple<DateTime, string>(data_entryA.Date, tag_a_name);

2 个答案:

答案 0 :(得分:1)

确定您可以格式化日期

var values = notEqualValue.Select(x => string.Join(",",x.Item1.ToString("d"),x.Item2));
this.reportTextBox.Text += String.Join(Environment.NewLine, values);

"d" format specifier以格式6/15/2009为您提供日期,但文化敏感。因此,如果您有不同的文化,请尝试使用DateTime.ParseExact方法和custom format string

答案 1 :(得分:0)

使用单字符格式 字符类型

您可以使用带有ToString或DateTime.ParseExact的单个char来指定预设格式。这些是标准格式,在许多程序中都很有用。它们可以消除自定义格式字符串中的拼写错误。 DateTime.Parse

class Program
{
    static void Main()
    {
    DateTime now = DateTime.Now;
    Console.WriteLine(now.ToString("d"));
    Console.WriteLine(now.ToString("D"));
    Console.WriteLine(now.ToString("f"));
    Console.WriteLine(now.ToString("F"));
    Console.WriteLine(now.ToString("g"));
    Console.WriteLine(now.ToString("G"));
    Console.WriteLine(now.ToString("m"));
    Console.WriteLine(now.ToString("M"));
    Console.WriteLine(now.ToString("o"));
    Console.WriteLine(now.ToString("O"));
    Console.WriteLine(now.ToString("s"));
    Console.WriteLine(now.ToString("t"));
    Console.WriteLine(now.ToString("T"));
    Console.WriteLine(now.ToString("u"));
    Console.WriteLine(now.ToString("U"));
    Console.WriteLine(now.ToString("y"));
    Console.WriteLine(now.ToString("Y"));
    }
}

输出

d    2/27/2009
D    Friday, February 27, 2009
f    Friday, February 27, 2009 12:11 PM
F    Friday, February 27, 2009 12:12:22 PM
g    2/27/2009 12:12 PM
G    2/27/2009 12:12:22 PM
m    February 27
M    February 27
o    2009-02-27T12:12:22.1020000-08:00
O    2009-02-27T12:12:22.1020000-08:00
s    2009-02-27T12:12:22
t    12:12 PM
T    12:12:22 PM
u    2009-02-27 12:12:22Z
U    Friday, February 27, 2009 8:12:22 PM
y    February, 2009
Y    February, 2009

所以最后你应该尝试

DateTime now = DateTime.Now;
    Console.WriteLine(now.ToString("d"));