gpt4 book ai didi

c# - 将日期时间格式更改为 "yyyy-mm-dd"

转载 作者:行者123 更新时间:2023-12-05 08:19:13 25 4
gpt4 key购买 nike

这个函数工作正常,但我返回 dd-MM-yyyy 格式,但我想要 yyyy-MM-dd 格式

我的输入值为 '13/5/2014 12:00:00 AM' 我需要将此格式更改为 '2014-5-13 00:00:00' 但所有日期时间变量都以 dd-mm- 返回yyyy 格式我不想将日期转换为字符串我想将日期值存储在日期时间属性中,格式为“yyyy-MM-dd”:

public DateTime DateConvertion(string Input)
{
DateTime DateValue = DateTime.ParseExact(Input, "dd-MM-yyyy", CultureInfo.InvariantCulture);
return DateValue;
}

最佳答案

来自 DateTime.ParseExact

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

在您的情况下,它们不是。

您可以改用 dd/M/yyyy hh:mm:ss tt 格式。这里有一个例子;

string s = "13/5/2014 12:00:00 AM";
var date = DateTime.ParseExact(s, "dd/M/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine(date);

DateTime 没有隐式格式,它只是一个 DateTime 值。您可以使用 DateTime.ToString() 方法将其格式化为 string

date.ToString("yyyy-M-dd hh:mm:ss");

看一看;

关于c# - 将日期时间格式更改为 "yyyy-mm-dd",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23608579/

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