简体   繁体   English

在C#中格式化日期时出现问题

[英]Issue while formatting date in c#

I'm passing TextBox DateTime value to Entity Model in api and formatting datetime into CultureInfo("en-GB", true) while saving data into database. 我将TextBox DateTime值传递给api中的实体模型,并将datetime格式化为CultureInfo(“ en-GB”,true),同时将数据保存到数据库中。

It is showing day as a month and month as a date. 它以天为月份,以月为日期。

Here is my code: 这是我的代码:

IFormatProvider FormatDate = new System.Globalization.CultureInfo("en-GB", true);

Convert.ToDateTime(user.DOB, FormatDate);

I'm passing date "08-04-2019" which is dd-MM-yyyy format, into user.DOB through api. 我将通过dd-MM-yyyy格式的日期“ 08-04-2019”传递到user.DOB中。

user.DOB showing Day = 4 and Month = 8 you can check it in the below image... user.DOB显示Day = 4和Month = 8您可以在下图中检查它...

日期时间值

I solved my issue by 我解决了我的问题

converting public DateTime DOB { get; set; } 转换public DateTime DOB { get; set; } public DateTime DOB { get; set; } public DateTime DOB { get; set; } to public string DOB { get; set; } public DateTime DOB { get; set; } public string DOB { get; set; } public string DOB { get; set; } public string DOB { get; set; } in DTO public string DOB { get; set; }在DTO中

then converted to DateTime with en-GB format while this into save procedure 然后将其转换为带有en-GB格式的DateTime,同时进入保存过程

IFormatProvider FormatDate = new System.Globalization.CultureInfo("en-GB", true);

Convert.ToDateTime(user.DOB, FormatDate);

您也可以这样做。

var mydate = new DateTime(1976, 10, 13);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM