thanks for all your help. it works on Date/Time. but, my date information is from SQL server database. When I get date from database. I want to dispaly it as short date like "10/02/2006" not "10/27/2006 12:00:00 AM". In VB, I use DateValue() to get short
date. I have function in my code as below. but it does not work. Also, if I wnat to add a day to it so that it will be "10/03/2006". how to do it?
xiuxian
Member
612 Points
194 Posts
Date Format in C#
Oct 03, 2006 06:49 PM|LINK
Hi,
I want to get date in "mm/dd/yyyy" format. I want short date. Can someone help?
thanks.
DarrellNorto...
All-Star
86809 Points
9646 Posts
Moderator
MVP
Re: Date Format in C#
Oct 03, 2006 06:58 PM|LINK
Console.WriteLine( dt.ToShortDateString() );
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
BasicDatePic...
Contributor
2037 Points
406 Posts
Re: Date Format in C#
Oct 03, 2006 09:50 PM|LINK
Hi xiuxian,
If you want to convert a DateTime object to a String and you want a specific format, try passing the format to the .ToString() Method. Example:
string myDate = DateTime.Today.ToString("MM/dd/yyyy");
Hope this helps.
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
tmitch
Member
238 Points
56 Posts
Re: Date Format in C#
Oct 04, 2006 12:57 AM|LINK
A quick reference for date formatting:
http://www.timmitchell.net/Blog/tabid/56/EntryID/20/Default.aspx
www.bucketofbits.com
www.tyleris.com
BasicDatePic...
Contributor
2037 Points
406 Posts
Re: Date Format in C#
Oct 04, 2006 08:48 AM|LINK
Hi xiuxian,
I forgot to mention we have a sample onine which lists all the .NET supported Cultures and several date and time formats for each Culture. See http://samples.basicdatepicker.com/cultureinfo.aspx
Hope this helps.
Culture datetime shortdate longdate shorttime longtime cultureinfo englishname
Basic Date Picker - A Quicker Picker(TM) - ASP.NET Calendar, Date and Time Web Controls
xiuxian
Member
612 Points
194 Posts
Re: Date Format in C#
Oct 04, 2006 01:29 PM|LINK
thanks for all your help. it works on Date/Time. but, my date information is from SQL server database. When I get date from database. I want to dispaly it as short date like "10/02/2006" not "10/27/2006 12:00:00 AM". In VB, I use DateValue() to get short date. I have function in my code as below. but it does not work. Also, if I wnat to add a day to it so that it will be "10/03/2006". how to do it?
string getSubmittedeDate(object insubmitteddate) {
string subdate="";
subdate = insubmitteddate.ToString("MM/dd/yyyy");
return subdate;
}
please help. thanks.
tmitch
Member
238 Points
56 Posts
Re: Date Format in C#
Oct 04, 2006 01:37 PM|LINK
Even if you are using the long date format from SQL Server, you can still cast this value to a .NET DateTime and address it programmatically.
DateTime dt = DateTime.Parse(my_date_time_value_from_sql);
return dt.ToString( <your formatting string here> );
Hope this helps.
www.bucketofbits.com
www.tyleris.com
rajaron
Member
657 Points
202 Posts
Re: Date Format in C#
Oct 04, 2006 01:51 PM|LINK
try this...
DateTime dt = Convert.ToDateTime(getSubmittedeDate);
string format = "dd/MM/yy";
String date;
date = dt.ToString(format,DateTimeFormatInfo.InvariantInfo);
xiuxian
Member
612 Points
194 Posts
Re: Date Format in C#
Oct 04, 2006 04:36 PM|LINK
got it. thank you so much.
vamsirays
Member
2 Points
1 Post
Re: Date Format in C#
Feb 06, 2013 06:09 AM|LINK
thank you very much it helps