简体   繁体   中英

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.

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.

user.DOB showing Day = 4 and Month = 8 you can check it in the below image...

日期时间值

I solved my issue by

converting public DateTime DOB { get; set; } public DateTime DOB { get; set; } public DateTime DOB { get; set; } to public string DOB { get; set; } public string DOB { get; set; } public string DOB { get; set; } in DTO

then converted to DateTime with en-GB format while this into save procedure

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

Convert.ToDateTime(user.DOB, FormatDate);

您也可以这样做。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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