0

Possible Duplicates:
Convert string to datetime Using C#
Converting String to DateTime C#.net

Hi, I have a string which consists of datetime in the format dd/mm/yyyy hh:mm:ss.ms

i.e. date/month/year hours:minutes:seconds.milliseconds. eg. 18/03/2011 15:16:57.487

How do I convert this string to datetime using c#

Regards cmrhema.

Community
  • 1
  • 1
cmrhema
  • 981
  • 2
  • 16
  • 28
  • 3
    You *did* notice the *Related Questions* list that appeared when writing your question? There were plenty of exact duplicates of your question right there. – Fredrik Mörk Mar 15 '11 at 15:51
  • Before posting why don't you search for the previous post that are listed while posting the question – Developer Mar 15 '11 at 15:51
  • 1
    Searching again, I found this to be better (since it also points out `DateTime.TryParse`): [Convert string to DateTime in C#](http://stackoverflow.com/questions/1592653/convert-string-to-datetime-in-c/1592660#1592660) – Fredrik Mörk Mar 15 '11 at 15:55
  • 2
    I swear there needs to be a DateTime.ParseExact tag, as this question comes up almost daily. – Massif Mar 15 '11 at 15:55

7 Answers7

9

You can use DateTime.Parse or DateTime.ParseExact

  DateTime dateValue;
  string dateString = "2/16/2008 12:15:12 PM";
  try {
     dateValue = DateTime.Parse(dateString);
     Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue);
  }   
  catch (FormatException) {
     Console.WriteLine("Unable to convert '{0}'.", dateString);
  }

For ParseExact

dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
try 
{
     result = DateTime.ParseExact(dateString, format, provider);
     Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) 
{
     Console.WriteLine("{0} is not in the correct format.", dateString);
}
Volem
  • 616
  • 3
  • 15
4

You're looking for DateTime.ParseExact:

DateTime time = DateTime.ParseExact(
    "18/03/2011 15:16:57.487", 
    "dd/MM/yyyy HH:mm:ss.fff", 
    CultureInfo.InvariantCulture
);

Note that MM means month, mm means minute, HH is 24-hour hour, hh is 12-hour hour, and f is millisecond.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

DateTime.ParseExact is what you want if you know the exact format of the date string.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
2

Here is a way to parse in the format that your DateTime is in and also maintain the milliseconds

        string dtString = "18/03/2011 15:16:57.487";

        System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");           

        DateTime dt = DateTime.Parse(dtString.Split('.')[0], culture);

        Double milliseconds = Double.Parse(dtString.Split('.')[1]);

        dt = dt.AddMilliseconds(milliseconds);
Wes Grant
  • 2,044
  • 16
  • 14
1

Look at DateTime.Parse()

Developer
  • 8,390
  • 41
  • 129
  • 238
0

Convert.ToDateTime() http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx

jwatts1980
  • 7,254
  • 2
  • 28
  • 44
0
String str="04031985";

DateTime dt = DateTime.Parse(str);
DateTime dt = DateTime.ParseExact(str, @"MMddyyyy", null);
Venki
  • 1,419
  • 5
  • 28
  • 38