locked
Formatting datetime RRS feed

  • Question

  • Hi- I am trying to insert a date into a table.  Currently, I have the date in MM-dd-yyyy format but i need yyyy-MM-dd format.  I have:

     DateTime TxtDateEditedFormatted;
            DateTime.TryParse(TxtDateEdited.Text, out TxtDateEditedFormatted);
            DateTime TxtScheduleDoneDateFormatted;
            DateTime.TryParse(TxtScheduleDoneDate.Text, out TxtScheduleDoneDateFormatted);
            DateTime TxtAstraDoneDateFormatted;
            DateTime.TryParse(TxtAstraDoneDate.Text, out TxtAstraDoneDateFormatted);
            DateTime TxtITCHDoneDateFormatted;
            DateTime.TryParse(TxtITCHDoneDate.Text, out TxtITCHDoneDateFormatted);
            DateTime TxtProofCopyDoneDateFormatted;
            DateTime.TryParse(TxtProofCopyDoneDate.Text, out TxtProofCopyDoneDateFormatted);


    How do I format it to yyyy-MM-dd?  Thank you
    Monday, December 7, 2009 3:52 PM

Answers

  • Using your last DateTime variable:  TxtProofCopyDoneDateFormatted.ToString("yyyy-MM-dd");
    • Proposed as answer by syntaxeater Tuesday, December 8, 2009 6:13 AM
    • Marked as answer by eryang Monday, December 14, 2009 3:30 AM
    Monday, December 7, 2009 4:11 PM
  • Hi,
    you can format your DateTime object using String property

                String datestr = String.Format("{0:dd_MMMM_yyyy_hh_mm_ss_tt}", currentDate);

    • Marked as answer by eryang Monday, December 14, 2009 3:30 AM
    Tuesday, December 8, 2009 6:08 AM

All replies

  • Using your last DateTime variable:  TxtProofCopyDoneDateFormatted.ToString("yyyy-MM-dd");
    • Proposed as answer by syntaxeater Tuesday, December 8, 2009 6:13 AM
    • Marked as answer by eryang Monday, December 14, 2009 3:30 AM
    Monday, December 7, 2009 4:11 PM
  • Hi,
    you can format your DateTime object using String property

                String datestr = String.Format("{0:dd_MMMM_yyyy_hh_mm_ss_tt}", currentDate);

    • Marked as answer by eryang Monday, December 14, 2009 3:30 AM
    Tuesday, December 8, 2009 6:08 AM