gpt4 book ai didi

c# - 迄今为止的字符串 - 在 C# [ "Mon Jan 13 2014 00:00:00 GMT+0000 (GMT Standard Time)"] 中

转载 作者:太空狗 更新时间:2023-10-29 20:47:29 26 4
gpt4 key购买 nike

我有一种情况,我收到以下格式的字符串形式的日期。

“2014 年 1 月 13 日星期一 00:00:00 GMT+0000(GMT 标准时间)”

我需要在 c# 中将其转换为以下格式(日期/字符串)以进行进一步处理

YYYY-MM-DD (2014-01-13)

Convert.ToDateTime(SelectedData)    

以上代码报错如下:

'Convert.ToDateTime(SelectedData)' threw an exception 
of type 'System.FormatException' System.DateTime {System.FormatException}

有什么建议吗?

我无法更改接收日期的格式最好的问候。

最佳答案

您将需要使用 DateTime.ParseExact:

var date = DateTime.ParseExact(
"Mon Jan 13 2014 00:00:00 GMT+0000 (GMT Standard Time)",
"ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'",
CultureInfo.InvariantCulture);

解析日期后,您可以按照自己的方式发送:

date.ToString("yyyy-MM-dd");

这是一个 Ideone来证明。

关于c# - 迄今为止的字符串 - 在 C# [ "Mon Jan 13 2014 00:00:00 GMT+0000 (GMT Standard Time)"] 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21138864/

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