locked
how to convert dd/mm/yyyy to dd MMM yyyy using c#

    Question

  • Hi,

     

    I have a date  "24-07-2012"

    I want to convert it into dd mm yyy format (24 Jul 2012 format)

    can u plz suggest the code

    Thursday, August 16, 2012 5:03 AM

Answers

  • If you are looking for the Exact format(means without / ),then you can try the below code :

     string dt2 = DateTime.Today.ToString("dd/MMMM/yyyy");
     dt2 =dt2.Replace('/', ' ');


    Thanks and Regards -Rajendra

    Thursday, August 16, 2012 6:06 AM

All replies

  • Hi Divya,

    try the below code :

    string

    dt2 = DateTime.Today.ToString("dd/MMMM/yyyy");


    Thanks and Regards -Rajendra

    • Proposed as answer by pankaj.nith Friday, August 17, 2012 1:25 AM
    Thursday, August 16, 2012 5:53 AM
  • If you are looking for the Exact format(means without / ),then you can try the below code :

     string dt2 = DateTime.Today.ToString("dd/MMMM/yyyy");
     dt2 =dt2.Replace('/', ' ');


    Thanks and Regards -Rajendra

    Thursday, August 16, 2012 6:06 AM
  • Sorry for the late reply, but I just saw your post.  I use date formatting a lot.  Since I use dynamic input data, I have to update date formats on-the-fly.  The code I'm using has a whole series of different date formats I can use, depending on what input is used or what output is desired.  Here's how I do it...

    Set up a date variable...

      string dateFormat;

    In this code sample, key is the variable being used in the test script and value is the data I want to use to populate the edit field.  The code cycles through the list of variables until it finds the correct one (where key and value match).

    At the start, the input data is the name of the variable where the actual date will be stored.

    For example:
      key   = strDate_MMSddSyyyy
      value = strDate_MMSddSyyyy

    When the match is found, the code is basically saying, "Oh, here's a variable I need to set."  The veriable gets reset to an actual date like this:

      // Use the current time...
      DateTime = DateTime.Now;
      
      // Identify the date format to use...
      if (key.Contains("MMSddSyyyy")
        dateFormat = "MM/dd/yyyy";
      // Reset the date to the desired format...
      value = value.Replace(key, time.ToString(dateFormat));

    value gets updated from strDate_MMSddSyyyy to 08/31/2012.

     The same can be done for any format.  If you use dateFormat = "dd MMM yyyy"; then the date would be formatted as 31 Aug 2012.


    Kevin

    Friday, August 31, 2012 7:58 PM