1

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...

datetime values

Shreyas Pednekar
  • 1,285
  • 5
  • 31
  • 53
  • So `DOB` is a string? – Erik Philips Apr 09 '19 at 09:57
  • 5
    use `DateTime.Parse` or `DateTime.ParseExact` – TheGeneral Apr 09 '19 at 09:57
  • @ErikPhilips no, DOB is a DateTime – Shreyas Pednekar Apr 09 '19 at 10:04
  • Possible duplicate of [DateTime and CultureInfo](https://stackoverflow.com/questions/13797727/datetime-and-cultureinfo) – Ian Preglo Apr 09 '19 at 10:12
  • @IanP. Nope, My question is different. Day & Month was showing wrong after formatting. – Shreyas Pednekar Apr 09 '19 at 10:19
  • The best way is passing date in UTC format, then use parameter in the insert/update query. So that you always work with the same format. Database cares of his culture. Same is for textbox. – Lorenzo Isidori Apr 09 '19 at 10:21
  • 1
    @ShreyasPednekar The code provided works as expected. Can you confirm if the input is a string, it's value is as you expect? – Kami Apr 09 '19 at 10:26
  • Is user.DOB a DateTime object or a string? If is is already a DateTime then why are you using Convert.ToDateTime() method? – jdweng Apr 09 '19 at 11:11
  • @Kami yes, the input is a string and it is coming from angular app through api – Shreyas Pednekar Apr 10 '19 at 06:45
  • @jdweng user.DOB is already a DateTime, but I wanted to format it in CultureInfo("en-GB", true) while inserting the record into database. – Shreyas Pednekar Apr 10 '19 at 06:46
  • What you did does absolutely nothing. The formatting is not stored in variable. The DateTime is store as a number with the whole portion of the number is the number of day from 1/1/1 and the fraction is a fraction of 24 hours (.25 is 6 hours, .5 is 12 hours, and .75 is 18 hours). Changing the culture is probably changing the timezone which is going to affect the hours and may cause the Day to change. – jdweng Apr 10 '19 at 08:41
  • @jdweng Okay then tell me why `DateTime.Parse(user.DOB.ToString(), FormatDate)` showing correct day and month but `Convert.ToDateTime(user.DOB, FormatDate)` showing swapped day and month? – Shreyas Pednekar Apr 10 '19 at 08:56
  • What did you start with? If you didn't convert you wouldn't of had any issue. The conversion from a string to a DateTime is using the computer local settings (including culture). Your code may of been overriding the local settings on you were running code in US but the data was collected in England. If you didn't convert in the first place there wouldn't of been any issue because the Date Would of been stored as a number in UTF. – jdweng Apr 10 '19 at 09:44
  • @ShreyasPednekar See - https://learn.microsoft.com/en-us/dotnet/api/system.convert.todatetime?view=netframework-4.7.2 . If the supplied value is a `DateTime`, then the specified DateTime object is returned; no actual conversion is performed. What are you expecting the output from this function to be? Would `user.DOB.ToString("dd/MM/yyyy")` not be sufficient? – Kami Apr 10 '19 at 09:55

2 Answers2

0

I solved my issue by

converting public DateTime DOB { get; set; } to 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);
Shreyas Pednekar
  • 1,285
  • 5
  • 31
  • 53
-3

You can also do something like this.

var mydate = new DateTime(1976, 10, 13);
eosishtar
  • 1
  • 2
  • 1
    Not sure how this helps, OP is asking about having the date and month swapped when parsing a date! – phuzi Apr 09 '19 at 12:19