KHV
KHV

Reputation: 145

Conversion of DOB from mmddyy to mmddyyyy

I am facing issue while trying to convert the DOB for certain range of years, the conversion logic that I am using is,

DateTime.ParseExact("211290", "mmddyy", DateTimeFormatInfo.InvariantInfo).ToString("mmddyyyy")

If the DOB that is 211290 I am getting the correct result as 12211990. In the same way if I change the DOB to 071429 getting the result as 07142029.

Can anybody let me know if I am making any mistake at the time of conversation. Please let me know, as this is a Production issue we are in a critical stage.

Thanks & Regards

Harsha

Upvotes: 0

Views: 235

Answers (1)

G3nt_M3caj
G3nt_M3caj

Reputation: 2685

First of all format string patterns are case sensitive/camelCase, that means mmddyy is not MMddyy as mm represent minutes and MM months. Secondly assuming you are trying to transform just a date input (with not a time) you can use Date.ParseExact rather Datetime.ParseExact (but ok that remains optionally) and more important in that is that 211290 will never be a MONTH/DAY/YEAR(2 places) as a 21°-th Month doens’t exist. For more info you can see: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=net-5.0

'WRONG INPUT    
'WRONG INPUT NEEDS THAT you have to change MMddyy in ddMMyy or 211290 in 122190:
'Console.WriteLine(DateTime.ParseExact("211290", "MMddyy", DateTimeFormatInfo.InvariantInfo).ToString("MMddyyyy"))

'As follows
Console.WriteLine(DateTime.ParseExact("211290", "ddMMyy", DateTimeFormatInfo.InvariantInfo).ToString("MMddyyyy"))
Console.WriteLine(DateTime.ParseExact("122190", "MMddyy", DateTimeFormatInfo.InvariantInfo).ToString("MMddyyyy"))

Upvotes: 2

Related Questions