Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert.ToDateTime(String, IFormatProvider) Method in C#
The Convert.ToDateTime(String, IFormatProvider) method in C# converts a string representation of a date and time to an equivalent DateTime object, using the specified culture-specific formatting information.
This method is particularly useful when working with date strings from different cultures or regions, as it allows you to specify how the date should be interpreted based on cultural formatting conventions.
Syntax
Following is the syntax −
public static DateTime ToDateTime(string value, IFormatProvider provider);
Parameters
-
value − A string that contains a date and time to convert.
-
provider − An object that implements
IFormatProviderand supplies culture-specific formatting information. Typically aCultureInfoobject.
Return Value
Returns a DateTime object that represents the date and time specified by the value parameter, interpreted according to the culture information provided by the provider parameter.
Using Convert.ToDateTime with US Culture
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
CultureInfo cultures = new CultureInfo("en-US");
String val = "11/11/2019";
Console.WriteLine("Converted DateTime value...");
DateTime res = Convert.ToDateTime(val, cultures);
Console.Write("{0}", res);
}
}
The output of the above code is −
Converted DateTime value... 11/11/2019 12:00:00 AM
Using Convert.ToDateTime with Different Cultures
Example
using System;
using System.Globalization;
public class CultureDemo {
public static void Main() {
string dateString = "15/03/2023";
// Parse with UK culture (dd/MM/yyyy format)
CultureInfo ukCulture = new CultureInfo("en-GB");
DateTime ukDate = Convert.ToDateTime(dateString, ukCulture);
Console.WriteLine("UK format: " + ukDate.ToString("dd/MM/yyyy"));
// Parse with French culture
CultureInfo frCulture = new CultureInfo("fr-FR");
DateTime frDate = Convert.ToDateTime(dateString, frCulture);
Console.WriteLine("French format: " + frDate.ToString("dd/MM/yyyy"));
// Using invariant culture for ISO format
string isoDate = "2023-03-15";
DateTime isoDateTime = Convert.ToDateTime(isoDate, CultureInfo.InvariantCulture);
Console.WriteLine("ISO format: " + isoDateTime.ToString("yyyy-MM-dd"));
}
}
The output of the above code is −
UK format: 15/03/2023 French format: 15/03/2023 ISO format: 2023-03-15
Common Culture Formats
| Culture | Date Format | Example |
|---|---|---|
| en-US | MM/dd/yyyy | 03/15/2023 |
| en-GB | dd/MM/yyyy | 15/03/2023 |
| de-DE | dd.MM.yyyy | 15.03.2023 |
| Invariant | yyyy-MM-dd | 2023-03-15 |
Conclusion
The Convert.ToDateTime(String, IFormatProvider) method enables culture-aware parsing of date strings, ensuring correct interpretation based on regional formatting conventions. This method is essential when working with internationalized applications or processing date data from different cultural contexts.
