Thomas Sampson

Dates!

Leave a comment

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

Advertisements

Author: tomtech999

I have recently graduated with a 1st class degree in MComp Games Software Development at Sheffield Hallam University, focusing primarily on application development in C++, with experience in graphics programming, scripting languages, DVCS/VCS and web technology. In my spare time I enjoy Drumming, Reading and Snowboarding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s