Results 1 to 9 of 9

Thread: Format Date

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Format Date

    Hi all,

    My question has to do with formatting a date returned in a datatable.
    Currently the column displays like this "2/4/1958 12:00:00 AM", but i would like for it to display like this "02/04/1958" so that i can set it to the text property of a textbox on a form.


    Thanks

    p.s. the sql query looks like this select bd, name from table where lname='Doe'
    the database it is querying is an oracle db, and the program is written in vs 2005.

  2. #2
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Format Date

    formattedDate = DateTime.Now.ToString("dd / MM / yyyy");

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Re: Format Date

    wouldnt that format the current date? I'd like to format a date that looks like
    "2/4/1958 12:00:00 AM"

    I tried the following
    DateTime bdate;
    bdate = (DateTime) myRow[2];
    textbox.text = bdate.ToShortDateString();

    and it doesnt keep the 0 in front of months and days
    i.e. 02/07/1954 comes out as 2/7/1954. is there a way to keep the leading 0's
    Last edited by RiverX; Dec 12th, 2007 at 05:00 PM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Re: Format Date

    Okay i figured it out

    DateTime bdate;
    bdate = (DateTime) myRow[2];
    text.Text = bdate.ToString("MM/dd/yyyy");

    is this the best way or is there another more efficient way?

    Thanks

  5. #5
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Format Date

    Well i made a litte application that contains two textboxes, and a button. One of the textbox contained your desired date;

    The button formats it to your desried format!

    Code:
    String t1 = (textBox1.Text);
                DateTime datex = System.DateTime.Parse(t1);
                String strDate = datex.ToString("dd /mm /yyyy");
                textBox2.Text = strDate;
    U can jst vary it from there

  6. #6
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Format Date

    if you just want it without the leading zero just do the following format:
    d/m/yyyy
    That should work i think

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    21

    Re: Format Date

    yeah thank you. I got it.
    Is this the best way or is there a more efficient way as well?


    Take care

  8. #8
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Format Date

    Im not sure whether that's the best way to be honest, that's the only way i can think of atm, haven't done C# in a while.

    I guess it depends on the context of what you're doing..

    Good luck

  9. #9
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    484

    Re: Format Date

    I tried this on a German machine, the formatting were wrong due to they use JJJJ and not YYYY for the year, and that gave me an error, and I wonder if some other country may be differ, the dd/mm/yyyy wont work already, is there another way?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width