gpt4 book ai didi

.net - 为什么 DisplayFormat 和 DataFormatString 将 "/"(斜杠)更改为 "-"(破折号)?

转载 作者:行者123 更新时间:2023-12-01 22:38:45 35 4
gpt4 key购买 nike

我正在使用以下代码

// Model
[DisplayFormat(
ApplyFormatInEditMode = true,
DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StartDate { get; set; }

// View
@Html.EditorFor(model => model.StartDate)

格式化StartDate,但结果是xx-xx-xxxx而不是xx/xx/xxxx。我该如何解决这个问题并始终使用 xx/xx/xxxx 格式?

更新:将文化更改为 en-US 似乎有效:

var culture = new CultureInfo(userCulture);
System.Threading.Thread.CurrentThread.CurrentCulture = "en-US";
System.Threading.Thread.CurrentThread.CurrentUICulture = "en-US";

但这不是解决方案,因为我可能使用不同的文化,并且我仍然想以不同的方式显示日期。

如果当前区域性要求日期应显示 dd-MM-yyyy,则如上所述使用 DisplayFormat 无效,并且日期不会像 那样显示日/月/年

最佳答案

改用DataFormatString = @"{0:dd\/MM\/yyyy}"。由于 / 标识应由当前区域性的默认日期分隔符替换的字符,因此您需要对其进行转义,以便将其用作格式中的文字。

这样您就拥有了固定格式,而不是动态使用当前区域性的日期分隔符的格式。

转义 / 字符的替代方法可以是:DataFormatString = "{0:dd'/'MM'/'yyyy}"

关于.net - 为什么 DisplayFormat 和 DataFormatString 将 "/"(斜杠)更改为 "-"(破折号)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8673174/

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