Community discussion forum

Date Format in "dd-MMM-yyyy"

Tags: .net, csharp, db India
  • 1 year ago

    ASP.NET (C#) :  

    DateTime dt;

    string fff;

    dt= DateTime.Parse("5/21/1984"); // dt=5/21/1984

    fff=dt.ToString("dd-MMM-yyyy"); // fff=21-May-1984

    dt=DateTime.Parse(fff); // dt=5/21/1984

    I need dt=21-May-1984. kindly help me plzzzzz. i want to store the date in "dd-MMM-yyyy" format in DateTime Variable (i.e.. dt)

  • 1 year ago
    You don't store a value in a DateTime in any format.  A DateTime simply contains a number.  As the MSDN documentation states:
    Time values are measured in 100-nanosecond units called ticks, and a particular 
    date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in 
    the GregorianCalendar 
    calendar. For example, a ticks value of 31241376000000000L represents the date, 
    Friday, January 01, 0100 12:00:00 midnight.
    Format is irrelevant.  Format is only an issue when converting a DateTime to a String or vice versa.  If you want to DISPLAY a DateTime in a particular format then you call the ToString method and specify the format, exactly as you're already doing.  You then display that string.
  • 1 year ago

    Actually i m binding the values fetched from SQL server to dataset and displaying in datagrid. if this data grid consist of any date fields (by default mm/dd/yyyy), then it has to be displayed in "dd-MMM-yyyy" format to the user in datagrid..........

    for (int i=0;i<ds.Tables[0].Rows.Count;i++)
         {     
         if (ds.Tables[0].Rows[i]["Check"].ToString()!="")
         {
          DateTime dt=DateTime.Parse(ds.Tables[0].Rows[i]["Check"].ToString());
          string fff;
          fff=dt.ToString("dd-MMM-yyyy");
          ds.Tables[0].Rows[i]["Check"]=fff;
          ds.AcceptChanges();
         }    }

     The above process is not updating in ds.tables[0].rows[i]["Check"]...

    Help me plz......................

  • 1 year ago

    Of course it isn't.  It can't possibly.  As I have told you already, a DateTime is stored as a number.  It has no format.  That code is, I'm afraid, a complete waste of time.  What you're doing there is converting a DateTime to a String to a DateTime to a String to a DateTime, which has the net effect of zero.  Your field contains exactly the same DateTime value as it started with, as it should.  When you then bind your data to your grid the binding process will call the ToString method of that DateTime value and the result will be displayed, which will be a string representation of that date and time in the default format.

    If you want your grid to display a different format then you have to specify that in the grid's properties.  What .NET version are you using?  Is this a Windows app or a Web app?  Once we know that we can tell you how to do this properly.

    Edit: Note that the process of converting between DateTime and String repeatedly may have the effect of zeroing the time portion, but the date will remain the same.  Regardless, you still end up with a DateTime value and DateTime values have no format.

  • 1 year ago

    ds.Tables[0].Columns.Add("FormattedCheck", typeof(string));

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    if (ds.Tables[0].Rows[i]["Check"].ToString() != "")
    {
    DateTime dt = DateTime.Parse(ds.Tables[0].Rows[i]["Check"].ToString());
    ds.Tables[0].Rows[i]["FormattedCheck"] = dt.ToString("dd-MMM-yyyy");
    }
    }

    But the thing is here all the fields fetching from database are dynamic (in the sense, user have provision to select the fields in order, so that if user specify the "check" field in a specified place (say, 4th column), then the "FormattedCheck" is to be placed in the same 4th column.

    help me plz?

    thanks in advance.

  • 1 year ago
    I've told you several times that what you're trying to do is not going to work, yet you're still posting code that's trying to force a formatted value into a DateTime.  In my last post I asked you two questions and told you that when you answered them I'd be able to help you.  You are ignoring the advice I'm providing and you're ignoring the questions I'm asking.  From this I can only conclude that you don't really want help, therefore I intend to offer no more.
  • 1 year ago
    Hmmm... now I look back at your first post and I see that it says ASP.NET.  Was that there the whole time?  If it was then you should have said "read my first post" when I asked.  Even so, you still haven't specified your version.  If you want someone to help and they ask for information the smart thing to do is answer them.
  • 1 year ago

    asp.net with C# (version 2003, 1.1 framework)

  • 1 year ago

    Kindly review my another blog .

    Subject : DateTime formatting in "dd-MMM-yyyy"???????

  • 1 year ago

    Hi Karan,

     If i understand your posting correctly...you can follow this methods instead of using for loop, At present your date format requirement is to select date from SQL and display into the datagrid.

    So you can format the date in the SQL query itself...you try this method...but in this method in betwen hyphen (-) wont come.

    ex:

     SELECT    Field1,Field2, CONVERT(Varchar(11), DateTimeField3, 106) AS _DtField From Table1

     and then fill your dataset and then bind your datagrid.

    I hope this may useful for you.

    Regards

    Hari K ......

     

     

  • 1 year ago

    Thanks Hari, it help me a lot.

    but can i get it with ( - ) Yes plz

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!