-
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.
-
Re: Format Date
formattedDate = DateTime.Now.ToString("dd / MM / yyyy");
-
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
-
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
-
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
-
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
-
Re: Format Date
yeah thank you. I got it. :D
Is this the best way or is there a more efficient way as well?
Take care
-
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
-
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?