Processing and saving past tense

I am having trouble choosing the best way to process and store time measurements.

I have a text box application that allows users to enter the time in hh: mm: ss or mm: ss format.

So, I planned to parse this line, tokenize it for colons and create a TimeSpan (or use TimeSpan.Parse () and just add β€œ00:” to the mm: ss case) for my business logic. Good?

How to store it like in a database? What will be the type of field? DateTime seems wrong. I do not want the time 00:54:12 stored as 1901-01-01 00:54:12, which seems a bit poor?

+6
c # database datetime timespan
source share
8 answers

TimeSpan has an Int64 Ticks property that you can save instead, and

+8
source share

I think the easiest way is to simply convert the user input to an integer number of seconds. So, 54:12 == 3252 seconds, so store 3252 in your database or anywhere. Then, when you need to display it to the user, you can return it again.

+3
source share

For periods not exceeding one day, just use the seconds, as others have said.

For longer periods, it depends on your db engine. If SQL Server, before version 2008 you need a date-time. This is normal - you can simply ignore the default date 1/1/1900, which they all will have. If you are lucky enough to have a sql 2008 server, then there are separate date and time data types that you can use. The advantage of using the real datetime / time type is the use of the DateDiff function to compare durations.

+3
source share

Most databases have a kind of time slot type. The answer depends on the database you are talking about. For Oracle, it's just a NUMBER floating point that represents the number of days (including fractional days). You can add / subtract this from / to any type of DATE and get the correct answer.

0
source share

As an integer number of seconds (or milliseconds)

0
source share

Do you collect both start time and stop time? If so, you can use the "timestamp" data type if your DBMS supports this. If not, exactly the same as the date / time type. Now you have said that you do not want the date to be saved - but consider the case where the time period spans midnight - you start at 23:55:01 and end at 00:05:14, for example - if you also have a date. There are standard build functions for returning elapsed time (in seconds) between two date and time values.

0
source share

Go with integers in seconds or minutes. Seconds are probably better. you will never kick yourself for choosing too much. Also, for your user interface, consider using multiple text inputs that you don’t need to worry about when the user actually typed ":" correctly. It is also much easier to add other restrictions, such as minutes and seconds, containing 0-59.

0
source share

and the int type should do this, keeping it in seconds and playing it back and forth

http://msdn.microsoft.com/en-us/library/ms187745.aspx

0
source share

All Articles