none
Date time format

    Question

  • Hi,

    I have a string containing date time as ,

    string  StrDate = '1995-02-02 01:05:03.000'

    I want to convert this string to datetime having a format as "yyyy-MM-dd HH:mm:ss".

    Any solution?

    Thanks..

    Saturday, September 15, 2012 6:16 AM

Answers

  • string stDate = "1995-02-02 01:05:03.000";
    DateTime dDate = DateTime.MaxValue;
    string stDate2 = "";
    if (DateTime.TryParse(stDate, out dDate) == true)
    {
        stDate2 = dDate.ToString("yyyy-MM-dd HH:mm:ss");
        Console.WriteLine("date ok: " + stDate2);
    }
    else
        Console.WriteLine("date NOT ok");
    


    Roar Jørstad Senior consultant http://www.evry.com

    Saturday, September 15, 2012 9:34 AM
  • Hi, use ParseExact method:

    string stDate = "1995-02-02 01:05:03.000";
    DateTime dDate = DateTime.ParseExact(stDate, "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
    string stDate2 = dDate.ToString("yyyy-MM-dd HH:mm:ss");


    Mitja

    Saturday, September 15, 2012 6:28 AM

All replies

  • Hello, You can use Parse Method of DateTime

    DateTime o = DateTime.Parse(string)

    http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    Saturday, September 15, 2012 6:27 AM
  • Hi, use ParseExact method:

    string stDate = "1995-02-02 01:05:03.000";
    DateTime dDate = DateTime.ParseExact(stDate, "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
    string stDate2 = dDate.ToString("yyyy-MM-dd HH:mm:ss");


    Mitja

    Saturday, September 15, 2012 6:28 AM
  • I try this

    ---------------------------------------------------------------

    string stDate = "1995-02-02 01:05:03.000"; DateTime dDate = DateTime.ParseExact(stDate, "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture); string stDate2 = dDate.ToString("yyyy-MM-dd HH:mm:ss");

    but it throws an exception

    for

    DateTime dDate = DateTime.ParseExact(stDate, "yyyy-MM-dd HH:mm:ss.fff",

    System.Globalization.CultureInfo.InvariantCulture);

    Exception : "String was not recognized as a valid DateTime."

    Saturday, September 15, 2012 6:36 AM
  • Hmm, it works for me.

    try to change last parameter of ParseExact (this is System.Globalization.CultureInfo.InvariantCulture) wth null (just type null).

    But it should work. You know why? Because I explicitly definded the string format of "date and time". 

    ---

    Q: Where do you come from, I guess not from Europe or North America!? Because this has to do wtih your local datetime settings (if it doesn`t work).


    Mitja

    Saturday, September 15, 2012 7:04 AM
  • string stDate = "1995-02-02 01:05:03.000";
    DateTime dDate = DateTime.MaxValue;
    string stDate2 = "";
    if (DateTime.TryParse(stDate, out dDate) == true)
    {
        stDate2 = dDate.ToString("yyyy-MM-dd HH:mm:ss");
        Console.WriteLine("date ok: " + stDate2);
    }
    else
        Console.WriteLine("date NOT ok");
    


    Roar Jørstad Senior consultant http://www.evry.com

    Saturday, September 15, 2012 9:34 AM