Wednesday, March 7, 2012

getting just the date or from DateTime field

Hi,
I have used smalldatetime datatype to store my date and time values. i want to store just the date or time but the problem is it stores both the date and time. For eg, if i add the the date 03/11/2004, it also the stores the current time automatically. so the new value will be something like 03/11/2004 10:00:00 AM where i want just 03/11/2004. further problem is even though i managed to store just the date like 03/11/2004 in the database, whole date and time shows up when i display it in my pages.

any help will be appreciated.

thanx,If i want to display just the date portion I use the following.
This is just using a datareader then putting the date in a label


While drDate.Read()
lblSent.Text = Format(drDate("datesent_column"), "dd/MM/yyyy")
End While

If you are in the US you might use "MM/dd/yyyy" instead of our aussie way.
It doesn't matter then if the whole date and time is in the column.
Hope this helps
Pete|||You should just use the more generic "short date formats" rather than hard-coding the format, but the general idea is sound.|||hi pkr,
even though i use short date formats, they will be in long date format when saved in database. i have used shortdatetime datatype in database as well.

seems like i need to format it again while displaying. is that the only solution?

thanx,|||SQL Server *only* supports date AND time. There's no getting around that fact, well not really anyway. My posting was talking about using the generic formats for rendering the values not storing them. You should always strive to use the users date formatting not impose one for them.

No comments:

Post a Comment