DateTime 用户声明,将日期格式设置为字符串(Asp.Net MVC 5)

DateTime User Claim, format date as string (Asp.Net MVC 5)

正在尝试将 DateOfBirth 类型的声明添加到我的 IdentityModels 文件中的 userIdentity。

public class ApplicationUser : IdentityUser
{
    public ICollection<Pet> Pets { get; set; }
    public System.DateTime DateofBirth { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        userIdentity.AddClaim(new Claim(ClaimTypes.DateOfBirth, this.DateofBirth));


        return userIdentity;
    }


}

代码 CS1503 的 2 个错误:

Argument1: cannot convert from 'string' to 'System.IO.BinaryReader' Argument2: cannon convert from 'System.DateTime' to 'System.Security.Claims.ClaimsIdentity'

显然我有类型错误,但我只是不知道如何将 DateTime 转换为字符串。

谢谢

在 .NET 中有多种标准方法可以将日期时间格式化为字符串。

  1. 使用 pre-specified 格式(即 DateOfBirth.ToString("s") 可排序格式(来源:.net docs
  2. 指定自定义格式(即 DateOfBirth.ToString("yyyy-MM-dd hh:mm:ss")(来源:.NET DateTime to String)。您可以取出片段并按预期更改 delimiters/surrounding 文本,但也有用于缩短的格式说明符格式化更小的时间单位。

为了将来参考,这个问题的更好标题可能是 "C#: How to convert datetime to string" 或另一个标题,它传达了让您卡住的部分。然后您会从了解您的问题的人那里获得更多意见。

根据方法的定义,其saying需要作为字符串添加