ASP.Net 2.0 C# DateTime IFormatProvider Using ParseExact

Updated on 21 May 2012,
Published on 20 Feb 2008

In the previous article C# Convert String to DateTime you learnt the use of Convert.ToDateTime function in C# ASP.Net 2.0. Following are 2 other methods to convert the DateTime format using IFormatProvider:

1. DateTime.ParseExact

2. DateTime.Parse

To use IFormatProvider you have to pass the culture info for DateTime format coz in different cultures DateTime format of displaying the sequence of month and date varies.

DateTime Format Examples:

You can see the live samples and examples of C# DateTime format from the following links:

For French Culture use the following:

IFormatProvider culture = new CultureInfo("fr-Fr", true);

For US English use the following:

IFormatProvider culture = new CultureInfo("en-US", true);

Both cultures mentioned above accept the different DateTime Formats.

If you are using DateTime.Parse then you have to pass the MM/dd/yyyy hh:mm:ss format of DateTime for French culture and dd/MM/yyyy hh:mm:ss for US English culture. You have to check the format for different cultures by changing the position of month and day.

Using DateTime Parse

dt = DateTime.Parse(myDateTimeString, culture, DateTimeStyles.NoCurrentDateDefault);

Using DateTime ParseExact

dt = DateTime.ParseExact(myDateTimeString, "MM/dd/yyyy hh:mm:ss", culture, DateTimeStyles.NoCurrentDateDefault);

C# Code to ParseExact the DateTime String

string myDateTimeString;

myDateTimeString = "19/02/2008 05:44:00";

IFormatProvider culture = new CultureInfo("fr-Fr", true);

dt = DateTime.ParseExact(myDateTimeString, "dd/MM/yyyy hh:mm:ss", culture, DateTimeStyles.NoCurrentDateDefault);

Response.Write(dt.Day + "/" + dt.Month + "/" + dt.Year);

Output:

You can see the outputs of above C# code from the following link:

DateTime Parse and ParseExact using IFormatProvider

Convert String to DateTime

Date Formats using ToString Function

Using C# Function to Get Current DateTime and C# ToString Function you can format the DateTime values into the specified format very easily.

10 Responses to "ASP.Net 2.0 C# DateTime IFormatProvider Using ParseExact"
aaditya sharma
This Code is not running. a error occured string(MM/dd/yyyy hh:mm:ss) is not valid DateTime Plz give me right solution.
Ezineasp.net
Hi Aaditya

I think you are getting error message due to wrong DateTime format string passed to the DateTime.ParseExact function.

If you are passing Date format as 30/06/2008 00:0:00 then use dd/MM/yyyy hh:mm:ss and if you are passing Date format string as 06/30/2008 00:00:00 then use MM/dd/yyyy hh:mm:ss.

Hope this will resolve your problem. Otherwise send your code to us for further consultation.
Lars Dahl
"dd/MM/yyyy hh:mm:ss" in not valid. As hh is 12 hours and it contain no AM/PM information, thus the format is ambiguous.

Try with "dd/MM/yyyy HH:mm:ss" instead. Here HH is 24 hours format.
asp
DD/MM/YYYY hh:mm:ss tt ->for 12 hours with am/pm DD/MM/YYYY HH:mm:ss ->for 24 hours DD - date MM - month YYYY - year HH - 24 hours hh - 12 hours mm - minutes ss - seconds tt - am/pm indication
nitish
it gives error dat system.DateTime is a type but is used like a variable.. why is it so??? plz tell in which datatype shld i store the date and time
Ezineasp.net
Hello Nitish

What is the syntax you are using to store the date and time values?
Nitish
thanks for showing so much concern i used d code as string date,time; date=(System.DateTime.Now.ToString).Value.Substring(0, 8); time=(System.DateTime.Now.ToString).Value.Substring(8, 12); DateTime dt,tm; dt=DateTime.ParseToExact(date); tm=DateTime.ParseToExact(time);
Ezineasp.net
Hi Nitish

You can learn about getting the date/time parts from the DateTime.Now method using following tutorials:

1. To get date:
C# Function to Get Current DateTime

2. To get time:
C# DateTime Format String

Hope this will help you.

Good luck ;)
Nitish
thanx sir.
Nitish
Thank you sir
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email