【问题标题】:Problem with the DateTime "0001-01-01T00:00:00"日期时间“0001-01-01T00:00:00”的问题
【发布时间】:2022-01-24 01:59:19
【问题描述】:

我的约会有问题。我和邮递员一起做插入,无论我输入什么,我总是返回“0001-01-01T00:00:00”。

还想将输出限制为日期。

非常感谢您的帮助

谢谢

Model:

public class BusinessTripDocument
    {
        

        public long Id { get; set; }
        public string StartLocation { get; set; }
      [JsonPropertyName("startDateTime")]
        public DateTime StartDate { get; set; }
        public string Destination { get; set; }
        public DateTime DestinationDate { get; set; }
        public string Transportation { get; set; }
        public string SuperiorsApproval { get; set; }
// POST: api/Businesstripdocuments
       
        [HttpPost("new/{eId}")]
        public async Task<ActionResult<BusinessTripDocument>> PostBusinesstripdocuments(long eId, BusinessTripDocument businesstripdocuments)
        {
            Employee employee = await _context.Employees.FindAsync(eId);
            //  businesstripdocuments.Employee = employee;
            //   _context.Businesstripdocuments.Add(businesstripdocuments);
            employee.addDocument(businesstripdocuments);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetBusinesstripdocuments", new { id = businesstripdocuments.Id }, businesstripdocuments);
        }

Postman

【问题讨论】:

    标签: c# asp.net datetime model format


    【解决方案1】:

    仅从您显示的代码来看 - 在我看来您应该只需要为您的模型提供 DataType 属性?

    //using System.ComponentModel.DataAnnotations;
    public class BusinessTripDocument 
    {
        ...
        [DataType(DataType.Date)]
        [JsonPropertyName("startDateTime")]
        public DateTime StartDate { get; set; }
        ...
    }
    

    【讨论】:

    • 对不起,我忘记显示 using,但 System.ComponentModel.DataAnnotations 正在使用中
    • @Notte ,我只添加了注释 using 以显示要使用哪个以添加 [DataType(DataType.Date)] 属性。
    【解决方案2】:

    我建议你可以做两种方式: 1.- 日期可以在 PostBusinesstripdocuments 方法中 2.- 日期可以是 BusinessTripDocument 类中的字符串,在转换为日期时间之后。

    她就是一个例子:

    【讨论】:

    • 我怎样才能捕捉到日期类型的字符串,它是商务旅行文件吗?我认为字符串始终具有相同的日期“0001-01-01T00:00:00”
    猜你喜欢
    • 2022-07-25
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2020-07-17
    相关资源
    最近更新 更多