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

Date Time issue

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB
Hi,

I have a datetime issue

At the moment I get a time value as '16/09/2010 06:00:00' but in my database the time value is 600. Is there anyway I can convert the datetime in my application to be passed to the database as 600 which is 6 am or can i take care of the problem at the backend. I'm using sql server 2005.

Thanks in advance
 
So, to clarify:

You have a "string" value being stored in the database and not a datetime value?

Your 600 equates to 6:00 am so does that mean that 654 equates to 6:54 am? And what about 1:34 pm? Is that stored are 134 or as 1334 (this one I think)?

Is it possible to change this datatype in the database to be a datetime instead of the string I suspect it is? That will be the easiest solution as it would recquire almost nothing in your application to do any conversions. Since you are using SQL2005, there is not Time datatype (introduced in SQL2008). But you can still use the DateTime datatype and just ignore the date portion. Or always set the date portion to some fictitious number.

If my above assumptions are correct and you can't change the datatype in the database, then you will have to parse this data back and forth between your database and application. You will need to get the string value of the time from your field (easy enough to do DateTime.Now.ToShortTimeString() - with maybe a bit of formatting needed) and when returning to the application from the database you will have to use something like DateSerial to create the date/time combination from the string value in the database.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Thanks mstrmage1768

I have managed to convert from a date to a string using (HHmm)
the problem is converting back as i need the time to be displayed again but using
Code:
 {
    ID = ((RadTimePicker)cntrl).ID;
    if (Session[ID] != null)
                   
    ((RadTimePicker)cntrl).SelectedDate = Convert.ToDateTime(Session[ID]);

i get the error message String was not recognized as a valid DateTime.
 
I tried

Code:
Convert.ToDateTime(DateTime.Now.ToString("dd/MM/YYYY") + " " + Session[ID].ToString().Substring(0,2) + ":" + Session[ID].ToString().Substring(2,2));
and got
The string was not recognized as a valid DateTime. There is an unknown word starting at index 6
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top