我正在尝试在以下模型中手动将字符串设置为有效的日期时间:
namespace Application.Models
{
public class Tienda
{
...
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime tienda_fecha_fin { get; set; }
...
通过控制器:
[HttpPost, ActionName("Create")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(int? id, StoreIndexData AddStore)
{
if (ModelState.IsValid)
{
var store = new Tienda();
store.tienda_fecha_fin = DateTime.ParseExact("31/12/9999 12:00:00", "0:dd/MM/yyyy", CultureInfo.InvariantCulture);
我尝试了不同的方法,因为我不记得0:dd/MM/yyyy
期望的确切格式是什么。
我试过:
9999/12/31; 31/12/9999 12:00:00 上午; 9999-12-31 12:00:00
到目前为止没有运气
您正在尝试将"31/12/9999 12:00:00"解析为"dd/MM/yyyy">
您应该使用:
DateTime.ParseExact("31/12/9999", "dd/MM/yyyy", CultureInfo.InvariantCulture);
或
DateTime.ParseExact("31/12/9999 12:00:00", "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
调用 DateTime.ParseExact,删除 format 参数的0:
部分。 这是调用string.Format
方法时在字符串模板中使用的占位符。
你想要:DateTime.ParseExact("31/12/9999", "dd/MM/yyyy", CultureInfo.InvariantCulture);