Ali Vojdanian
Ali Vojdanian

Reputation: 2111

datetime value in database

I updated the datetime to the datebase. It's 5/2/2012. But when I want to show this value from that database into a textbox it will show me : 05/02/2012.

Please help me to solve this problem. I want to show it's value into the textbox like the format has been updated : 5/2/2010

Thanks in advance

Upvotes: 1

Views: 1121

Answers (8)

user4629429
user4629429

Reputation:

Try this :

Textbox1.Text=datevalue.ToString("MM/dd/yyyy");

Upvotes: 1

Ali Vojdanian
Ali Vojdanian

Reputation: 2111

For doing that the best way is saving datetime into the database as a string. then after retrieving the string I can convert that to datetime. and it will show the correct format.

Upvotes: 0

EtherDragon
EtherDragon

Reputation: 2698

It's generally poor practice to go mucking with your data with a complex query or pre-format the data before reaching your application as you lose flexibility with the results.

The data coming from SQL is properly formatted as a generic DateTime stamp - allow the application to massage it for display purposes, as @Chris-Sinclair suggests with the link to examples.

The best aproach, if using WPF, is to allow the UI element to format the text as it sees fit, that way the content of your class instances remains unmodified. It's still a DateTime object so you can do DateTime "math" or other manupiations without losing the full value.

Here is an example from elegantcode.com where the WPF UI element determines it's format:

<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy hh:mm tt}}" />

For your speficif purpose the correct line would read

<TextBlock Text="{Binding Date, StringFormat={}{0:M/d/yyyy hh:mm tt}}" />

Upvotes: 0

Allen
Allen

Reputation: 97

When you store this data into sql server as datetime type then the original format is lost. However, if you want to show the data without leading 0's then you can do this for your textbox.

mytextbox.Text = dt.Month.ToString().TrimStart('0') + "/" + dt.Day.ToString().TrimStart('0') + "/" + dt.Year.ToString()

Upvotes: 0

Krishanu Dey
Krishanu Dey

Reputation: 6406

You can use ToString("MM/dd/yyyy") while assigning the value.
If your textbox is Textbox1 and your date value is stored in DateTime object datevalue then you should do it like this.

Textbox1.Text=datevalue.ToString("MM/dd/yyyy");

Upvotes: 0

John Dewey
John Dewey

Reputation: 7093

SELECT CAST(DATEPART(MM,GETDATE()) AS VARCHAR(2))+'/'
+CAST(DATEPART(DD,GETDATE()) AS VARCHAR(2))+'/'
+CAST(DATEPART(YYYY,GETDATE()) AS VARCHAR(4))

Upvotes: 1

Chris Sinclair
Chris Sinclair

Reputation: 23198

If you have the data as a DateTime object, then you can format its output using its ToString(string) overload with the format string "d/M/yyyy" or any other format as described here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Upvotes: 2

Amit
Amit

Reputation: 22076

Try this ToString("d/M/yyyy") to give desired format.

If you can show me your code then I can help you more.

Upvotes: 0

Related Questions