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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date and Time displaying

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello,

I have to insert a current date and current time into a database.

I am using vb 2005 and sql 2005.

Code:
 Dim time As DateTime
 Dim day As DateTime

 time = Date.Now.ToShortTimeString()
 day = Date.Now.ToShortDateString()

I am using the above to get the time and the day. I would like to display the date as 1/11/2006 and the time as 10:08.

However, in the database it is showing as:
Date: 10/31/2006 12:00:00 AM
Time: 10/31/2006 9:34:11 PM

I have set the database field to nvarchar(50). I can't have the fields as dateTime, as I don't want to display a pop calandar in my ultragrid.

Is there a problem with the code?

Thanks in advance,

Steve
 
I don't understand why you can't have your field in SQL Server as DateTime. This has nothing to do with popup calendars in VB .Net as far as I know. Not unless you specifically bind one to the column when you call it.

If you stick with varchar, first shorten the length to varchar(10) so you can save some DB overhead. Then I think you need to convert your Dim'd variables to String before sending them to the database.

Code:
Dim Today as Datetime
Dim Day as string
Dim Time as string

Today = Now()

Day = Today.ToString("MM") & "/" & Today.ToString("dd") & "/" & Today.ToString("yyyy")

Time = Today.ToString("HH") & ":" & Today.ToString("mm")



Catadmin - MCDBA, MCSA
"No, no. Yes. No, I tried that. Yes, both ways. No, I don't know. No again. Are there any more questions?"
-- Xena, "Been There, Done That"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top