Date and Time
-
Hello, I have to insert a current date and current time into a database. I am using vb 2005 and sql 2005.
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 -
Hello, I have to insert a current date and current time into a database. I am using vb 2005 and sql 2005.
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, SteveUse Like this: Declaration Dim time As DateTime Dim day As DateTime Private culture As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("hi-IN") -------------------- time = Date.Now.ToShortTimeString() day = Date.Now.ToShortDateString() Dim temp As String = Convert.ToDateTime(time, culture) Dim var As String = Convert.ToDateTime(day, culture) This will convert the date and time into Indian culture. temp will have the value of 9:30:00 AM and var will have the value of 11/1/2006 Hope u will got it.
Janani
-
Hello, I have to insert a current date and current time into a database. I am using vb 2005 and sql 2005.
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, Stevesteve_rm wrote:
I can't have the fields as dateTime, as I don't want to display a pop calandar in my ultragrid.
Don't let limitations like that mess up your database design. Store the datetime properly in the database, and convert it to strings when you are fetching data to be used with that component.
steve_rm wrote:
Is there a problem with the code?
You have been "helped" by VB to convert the values back to DateTime. The extra conversions that are added, as you are storing the values in DateTime variables, are equivalent to: time = Convert.ToDateTime(Date.Now.ToShortTimeString()) day = Convert.ToDateTime(Date.Now.ToShortDateString()) A DateTime structure always has a date and a time, and no information about formatting. If you convert a string containing only a date to a DateTime, it will get the time 0:00. If you convert a string containing only a time, it will get the current date.
--- b { font-weight: normal; }