locked
DateTime format conversion RRS feed

  • Question

  • User84044094 posted

    Hi I have a string value that is retrieved from DB via sp call.I am trying to parse that to DateTime object.

     DateTime trainingDate = DateTime.Parse(dsProfiles.Tables[0].Rows[0]["TrainingDate"].ToString());

    Here dsProfiles.Tables[0].Rows[0]["TrainingDate"].ToString()) gives me in format yyyy-mm-dd.But after parsing to date time it change to dd-mm-yyyy.I tried using parse exact .Still gives the same value.How can i retain the date format after converting to dateTime??

    Thursday, December 17, 2015 7:38 AM

Answers

  • User-434868552 posted

    @sudheer60

    sudheer60

    I need the ouput again in Date time format in yyyy-mm-dd.

    Study the answer from oned_gk:

    string trainingDatestr = trainingDate.ToString("dd-MM-yyyy");

    simply swap "dd" with "yyyy":

    string trainingDatestr = trainingDate.ToString("yyyy-MM-dd");

    N.B.:    uppercase   MM  is months;    lowercase     mm     is minutes

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2015 6:19 PM

All replies

  • User-1716253493 posted

    AFAIK, DateTime is just a value, like a number

    You don't need to convert it to a string then parse it back to DateTime

    DateTime trainingDate = (DateTime)dsProfiles.Tables[0].Rows[0]["TrainingDate"];

    To represent the datetime value to spesific format, you can do like this

    string trainingDatestr = trainingDate.ToString("dd-MM-yyyy");

    Think that formated date is a string, the date dont have spesific format

    You can do like this

    string trainingDate = dsProfiles.Tables[0].Rows[0]["TrainingDate"].ToString("dd-MM-yyyy");

    Thursday, December 17, 2015 8:00 AM
  • User84044094 posted

    Hi ,I need the ouput again in Date time format in yyyy-mm-dd.

    Thursday, December 17, 2015 8:49 AM
  • User-434868552 posted

    @sudheer60

    sudheer60

    I need the ouput again in Date time format in yyyy-mm-dd.

    Study the answer from oned_gk:

    string trainingDatestr = trainingDate.ToString("dd-MM-yyyy");

    simply swap "dd" with "yyyy":

    string trainingDatestr = trainingDate.ToString("yyyy-MM-dd");

    N.B.:    uppercase   MM  is months;    lowercase     mm     is minutes

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2015 6:19 PM