locked
How to create a DateTime object with the user specified format? RRS feed

  • Question

  • Hi,

    How to create a DateTime object with user specified format like "yyyy-MM-dd HH:mm:ss" ?

    Thanks,

    Monday, January 11, 2016 2:11 AM

Answers

  • Hi, PM16

    You can supply format string in the ToString() function.

    For example: 

    DateTime newDateTime = new DateTime (2008, 9, 1,13,02,24 ); 

    then:

    string stringDateTime = newDateTime.ToString ( "yyyy-MM-dd HH:mm:ss" ); Wish that give you what you want.

    Please refer to the following page on MSDN for more information:   https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best regards

    Jason



    • Edited by Jason Jiangs Monday, January 11, 2016 3:04 AM
    • Proposed as answer by Barry Wang Tuesday, January 12, 2016 5:49 AM
    • Marked as answer by Barry Wang Tuesday, January 19, 2016 10:13 AM
    Monday, January 11, 2016 3:01 AM
  • try this:

    string input = "";
    DateTime value = DateTime.Parse(input);

    there is also an overload available which let you specify the input format


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Proposed as answer by Barry Wang Tuesday, January 12, 2016 5:49 AM
    • Marked as answer by Barry Wang Tuesday, January 19, 2016 10:13 AM
    Monday, January 11, 2016 7:19 AM

All replies

  • Hi, PM16

    You can supply format string in the ToString() function.

    For example: 

    DateTime newDateTime = new DateTime (2008, 9, 1,13,02,24 ); 

    then:

    string stringDateTime = newDateTime.ToString ( "yyyy-MM-dd HH:mm:ss" ); Wish that give you what you want.

    Please refer to the following page on MSDN for more information:   https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best regards

    Jason



    • Edited by Jason Jiangs Monday, January 11, 2016 3:04 AM
    • Proposed as answer by Barry Wang Tuesday, January 12, 2016 5:49 AM
    • Marked as answer by Barry Wang Tuesday, January 19, 2016 10:13 AM
    Monday, January 11, 2016 3:01 AM
  • Hi Jason,

    Thanks for your response.

    But i don't need a string of the DateTime. I need to create a DateTime Object itself.

    Thanks,
    Petchiammal.

    Monday, January 11, 2016 3:09 AM
  • try this:

    string input = "";
    DateTime value = DateTime.Parse(input);

    there is also an overload available which let you specify the input format


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Proposed as answer by Barry Wang Tuesday, January 12, 2016 5:49 AM
    • Marked as answer by Barry Wang Tuesday, January 19, 2016 10:13 AM
    Monday, January 11, 2016 7:19 AM