gpt4 book ai didi

c# - 使用标准或自定义日期格式

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:22 25 4
gpt4 key购买 nike

以下代码:

DateTime test = new DateTime();
Console.WriteLine(test.ToString("d"));
Console.WriteLine(test.ToString("d_"));
Console.WriteLine(test.ToString("dd"));

产生以下结果:

0001-01-01
1_
01

第一个结果是完整日期,因为 d 被视为标准日期格式。另一方面,d_ 被视为自定义日期格式,在这种情况下,d 被视为短日期字符串。有没有办法只生成没有下划线的短日?

最佳答案

任何这些:

Console.WriteLine(test.ToString("%d"));
Console.WriteLine(test.ToString(" d"));
Console.WriteLine(test.ToString("d "));

来自 https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#UsingSingleSpecifiers

To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before or after the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.

关于c# - 使用标准或自定义日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32865082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com