0

I have created an android application in which I have used android date picker. While submitting form in my application I am getting date in this format "18-2-2021" and I want to format this date in "18-Feb-2021" format. I don't want to force my user to update application so I want to format date in my c# web service code.

I am trying to format date using below code but I am getting "String was not recognized as a valid DateTime.".

DateTime requestRaisedDate = DateTime.ParseExact(ApplicationID.REQUEST_RAISED_DATE, "dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture); 

2 Answers 2

2

With 18-2-2021 you need to allow single-digit months: dd-M-yyyy:

DateTime requestRaisedDate = DateTime.ParseExact(ApplicationID.REQUEST_RAISED_DATE, "dd-M-yyyy",System.Globalization.CultureInfo.InvariantCulture); 

This allows 18-2-2021 and also 18-02-2021.

See: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#month-m-format-specifier

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. what will happen if I receive 25-10-2021 from mobile application because that time the month value is in two digit
0
DateTime requestRaisedDate = DateTime.ParseExact(ApplicationID.REQUEST_RAISED_DATE, "dd-M-yyyy",System.Globalization.CultureInfo.InvariantCulture); 

Use Single M for single-digit month. It will support 10, 11,12 by default. So, don't worry

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.