none
string to date format RRS feed

  • Question

  • Hello,

    I get an error as follows:
    The name 'DateTime' does not exist in the current context
    
    do you see why please?
    Thank you
    
    DateTime dt = DateTime.MinValue;
    dt = DateTime.ParseExact(DateTime.Now.ToString(), "yyyyMMdd HHmmss", System.Globalization.CultureInfo.InvariantCulture);

    Thursday, January 11, 2018 1:23 PM

Answers

  • It is highly unbelievable Now to String will return a string with a format yyyyMMdd HHmmss.

    But you have to check that using your local culture settings. 

    Or simple by using the debugger and this code. 

                DateTime dt = DateTime.MinValue;
                string x = DateTime.Now.ToString();
                dt = DateTime.ParseExact(x, "yyyyMMdd HHmmss", System.Globalization.CultureInfo.InvariantCulture);

    Beside that it is of course crazy code, because it implies that you set the DateTime first to 01/01/01/ 00:00:00

    And then to the current date in a fools way. 


    Success Cor



    • Edited by Cor Ligthert Thursday, January 11, 2018 1:33 PM
    • Marked as answer by arkiboys Thursday, January 11, 2018 4:06 PM
    Thursday, January 11, 2018 1:31 PM

All replies

  • It is highly unbelievable Now to String will return a string with a format yyyyMMdd HHmmss.

    But you have to check that using your local culture settings. 

    Or simple by using the debugger and this code. 

                DateTime dt = DateTime.MinValue;
                string x = DateTime.Now.ToString();
                dt = DateTime.ParseExact(x, "yyyyMMdd HHmmss", System.Globalization.CultureInfo.InvariantCulture);

    Beside that it is of course crazy code, because it implies that you set the DateTime first to 01/01/01/ 00:00:00

    And then to the current date in a fools way. 


    Success Cor



    • Edited by Cor Ligthert Thursday, January 11, 2018 1:33 PM
    • Marked as answer by arkiboys Thursday, January 11, 2018 4:06 PM
    Thursday, January 11, 2018 1:31 PM
  • "I get an error"

    As in a compiler error? The only way that would occur is if you removed the using System directive at the top of your .cs file. DateTime is defined in the System namespace.

    Note that if you used the common quick action to remove unused namespaces then it may remove System unless you're using a type from it. This is more common in C# because C# provides aliases for the common System types like Int32 and Boolean. 


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, January 11, 2018 2:54 PM
    Moderator