In this post, we will see how we can accept any date format and return with a common date format with the TryParseExact() method. Converting date to the wanted date format is a time spending and slow process in several languages including C#.
In the below code, I created one method namely ChangeDateFormat which accepts string date in any format and will return with fix format i.e. “MM/dd/yyyy” format.
public string ChangeDateFormat(string inputDate) { string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "d/MM/yyyy", "dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy", "MM/dd/yyyy","yyyy/MM/dd","dd/MM/yyyy","MM-dd-yyyy","yyyyMMdd"}; DateTime.TryParseExact(inputDate, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out DateTime outputDate); return outputDate.ToString("MM/dd/yyyy"); }
In the above method need to pass all possible format in string[] which we wanna accept. we have done now you can use this method anywhere in your project.
I hope you guys found something useful. Please give your valuable feedback/comments/questions about this article. Please let me know how you like and understand this article and how I could improve it.
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular