0
votes

I am using asp net core with entity framework. I have a datetimepicker which i am using as calendar. I have customize its format using jquery like this.

  $('.datepicker').datetimepicker({
        format: 'yyyy-mm-dd'       
    }); 

This shows the date forma to user. and then converting it to C# datetime I am saving it in database in same format.Every thing is correct till now. Now on my next visit this page has to be pre-filled if data is there. This time on the textbox of calendar it is showing some thing like 0007/02/19 where as the date that i entered is 1993/07/02 and the date that is saved in database is 7/2/1993. I tried using

            String DateString = Convert.ToString(ProfileDetails.DateOfBirth);
            IFormatProvider culture = new CultureInfo("en-US");

            DateTime dateVal = DateTime.ParseExact(DateString, "MM/dd/YYYY", CultureInfo.InvariantCulture);
            DateTime date = Convert.ToDateTime(dateVal);
            user.DateOfBirth = dateVal;

this but was getting error string is not in valid datetime format. In my modal I have defined date as datetime. I am not getting the solution. Can any one gives me the correct way to complete that.

1
Your post is rather confusing. You convert a date to a string in your first line of code but then do a bunch of other stuff that I don't quite follow. Can you explain what the further four lines of code are for?Chris
@Chris I too don't understand that code but i just fixed and posted an answer. Sometimes better don't askJ. Doe
@J.Doe: Except that your analysis falls flat as I've now indicated on a comment on your answer. I think it is quite plausible that the first line is generating a DateString like "07/02/1993" but because the date picker expects stuff in yyyy-mm-dd format it probably tries to be clever and parses the date as yyyy = 07, mm = 02, dd = 19. This is why knowing what the other code is there for would be very useful and why the OP should also make it clear how they are passing the data to the datepicker or the textbox.Chris

1 Answers

0
votes

Cuz you are using MM/dd/YYYY instead MM/dd/yyyy

Working code:

String DateString = Convert.ToString("07/02/1993");
IFormatProvider culture = new CultureInfo("en-US");
DateTime dateVal = DateTime.ParseExact(DateString, "MM/dd/yyyy", CultureInfo.InvariantCulture);