Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C#.net date format

Status
Not open for further replies.
Nov 29, 2001
72
US
Guys,
I haven't worked in C for awhile and I'm trying to remember how to do the following:

I have a date returned from a SQL table to a literal in my web app we'll call - EDate. This literal now contains the date but also a time. I want to eliminate the time portion and keep the date. This is a couple statements and simple as can be VB.net. However, trying to find how to do this in C# has proven most difficult. I can't seem to find anything helpful in MSDN (as usual) and I don't want to waste more time on what I consider a basic programming technique.

To anyone who can give me this answer, I promise a helpful star.

thanks in advance,
Dave
 
DateTime variables contain a date + time (as you've discovered ;-)). Formatting is the responsibility of outside code. To show just the date part, you can do something like this:
Code:
Console.WriteLine(MyDate.ToString("d"));
which will write the date value out in the short date pattern defined for the current thread's Culture. You can also specify a specifc data format (not recommended) by using something like:
Code:
Console.WriteLine(MyDate.ToString("yyyy-MM-dd"));

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
You can do this:

lblDate.Text = DateTime.Today.ToShortDateString();

Or in the HTML:

<asp:Label id="lblExpDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExpenseDate_DT", "{0:M/d/yyyy}") %>'>

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
You're sort of on the right track Chip. Here's exactly what I'm trying to do.

I'm retrieving data, including a date (see below), from a SQL table using a stored procedure.

wrk = tempDataRow["EDate"].ToString();

wrk now contains, we'll say, "7/2/2004 12:00:00". I want to now get rid of the time portion and move just the date to another string called EDate which I will use later on a form.

How in heck do I do this? I'm still searching MSDN with no luck.
 
Guys,
Thanks for your help, but I finally found the answer. If you're interested here's the code:

wrk = tempDataRow["EDate"].ToString();
indx = wrk.IndexOf(" ");
char[] DateArray = new char[indx];
wrk.CopyTo(0,DateArray ,0, indx);
tempDateText= new String(DateArray);

"wrk" starts with a "7/2/2004 12:00:00" date. the next statement moves the offset of " " to indx which happens to be 9. The next statement move the lenght value to DateArray. Then "wrk.CopyTo(0,DateArray, 0, indx);" copies the date (wrk) to the DateArray. And the final statement moves the date only "7/2/2004" to "tempDateText".

A lot of work to just strip off the time, but I'll take it.

Thanks again for your help,
Dave
 
Try this:
Code:
wrk = tempDataRow["EDate"].ToString("MM/dd/yyyy");
Less work.
;-)

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip,
I tried that and I got a compile error stating that the "ToString" function only has one parameter.

Thanks anyway,
Dave
 
I believe the reason why you get that error is because Chip's solution is for the .ToString() method of a DateTime object, not for your column's value.

Therefore, cast it to a DateTime first.

Code:
DateTime dt = Sytstem.Convert.ToDateTime(tempDataRow["EDate"]);
string EDate = dt.ToString("d");
MessageBox.Show(EDate);
 
Whoops, you're right.
In one line, it's:
Code:
Sytstem.Convert.ToDateTime(tempDataRow["EDate"]).ToString("d");
You'd need to check for DBNull, etc. before using the DataRow value.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top