Sunday, August 27, 2006

Dates!

I took this evening out to play aruond with dates n times , with regards to inserting and retrieveing and manipulating to and from a database.

fisrt things first, in a sql insert you can use Now() to put the date and time into most databases, provided that the filed is cast as a Date/Time field.

example

insert into mytable ('id','text',Now(),'helooo');


so now you have a workable date in your table.

This is fine but you want to bind them to a DateTime type in .net when you read them. This allows you to do example...

bindeddatetime.Hour
indeddatetime.ToShortDateString();

etc etc.

to do this, simply convert the data from your data reader

Example

while(mydatareader.Read())
{

DateTime thedate=Convert.ToDateTime(mydatareader["timefield"]);
//notice time field not converted to string at all, just binded straight to a DateTime
}
Conclusion

Your timestamp is now in a format ready to
  • manipulate,
  • sort ( ORDER BY DATE DESC)
  • add, time difference, etc
Associated links

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home