What's the simplest way to format a .NET DateT

2020-02-06 10:03发布

I have a DateTime class and wish to display it according to some format... in this case, I specifically want to format it as YYYYMMDD format.

What's the best C#/.NET API function for doing this?

5条回答
仙女界的扛把子
2楼-- · 2020-02-06 10:42
DateTime.Now.ToString("yyyyMMdd");
查看更多
祖国的老花朵
3楼-- · 2020-02-06 10:43
DateTime.ToString(<format string>)

Look at the docs for the format string details.

查看更多
太酷不给撩
4楼-- · 2020-02-06 10:48

I always use this site to get any dates formats etc. http://blog.stevex.net/index.php/string-formatting-in-csharp/

查看更多
闹够了就滚
5楼-- · 2020-02-06 10:49

ToString(format)? i.e.

string s = DateTime.Today.ToString("yyyyMMdd");

(note the case)

查看更多
在下西门庆
6楼-- · 2020-02-06 10:55
myDateTime.ToString("yyyyMMdd")

Here you can find a comprehesive list of DateTime patterns and pattern characters: DateTime.ToString() Patterns

查看更多
登录 后发表回答