gpt4 book ai didi

c# - 将日期时间转换为 yyyy/MM/dd

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

如何将日期时间对象转换为 yyyy/MM/dd 格式?我正在尝试这个:

DateTime selectedDate =Convert.ToDateTime( Calendar1.SelectedDate.ToShortDateString());
selectedDate = DateTime.ParseExact(selectedDate, "yyyy/MM/dd", CultureInfo.InvariantCulture);

但是我得到以下错误:

The best overloaded method match for 'System.DateTime.ParseExact(string, string, System.IFormatProvider)' has some invalid arguments

Argument '1': cannot convert from 'System.DateTime' to 'string'

最佳答案

DateTime 没有固有格式 - 只有内部表示。

当您想要显示 DateTime 实例的值时,格式化就会发挥作用:

string formatted = DateTime.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);

上面的 formatted 字符串将包含所需格式的 DateTime 实例的值。

关于c# - 将日期时间转换为 yyyy/MM/dd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9498049/

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