Please enable Javascript for better experience...
Parsing string date in dd/mm/yyyy format into Datetime
By Rahul Kumar Jha | May 2, 2020 | In Tips | Total Views [ 3705 ]
Taged In
(0 Like)
Rate

By default date understand mm/dd/yyyy format. You can parse string date into dd/mm/yyyy using different options.

Introduction

In this article we will see the options to change string date in dd/mm/yyyy format into DateTime. If you try to convert a date in dd/mm/yyyy (say 17/03/2020), it will throw exception "FormatException: String was not recognized as a valid DateTime". To overcome, follow anyone from below methods.

Converting string format (dd/mm/yyyy) into Date

Use anyone of below method and you are done.

void ConvertDate(string date)
{
if (!string.IsNullOrWhiteSpace(date))
{
string[] splittedDate = date.Split('/');
DateTime date1 = new DateTime(Convert.ToInt32(splittedDate[2]),
Convert.ToInt32(splittedDate[1]), Convert.ToInt32(splittedDate[0]));
DateTime date2 = Convert.ToDateTime(date,
System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
DateTime date3 = DateTime.ParseExact(date, @"d/M/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
DateTime date4;
bool isSuccess = DateTime.TryParse(date, out date4);
}
}

Hope this helps you.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors