Red Swan
Red Swan

Reputation: 15563

assign time to variable

this question maybe foolish by me. but i want to understand the standards.

I am creating the online exam application. i want to assign the time for each question while its insertion through UI. what field i have to keep there (on UI? and in code behind? and in database?) ? Timespan ? or date time ? . if i keep time span then how do i can convert timespan to datetime while loading the set of questions?I am using asp.net mvc and c#

Upvotes: 1

Views: 4611

Answers (2)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60744

TimeSpan is the time elapsed between two DateTimes.

A DateTime is a specific time and date, but has no understanding of time usage.

Therefore DateTime is the correct to use for you.

EDIT

According to your response on another answer, it's the duration you are after, and that is contained inside a TimeSpan. If it's minutes, the easiest approach is to use:

TimeSpan myTimeSpan = TimeSpan.FromMinutes( myUserInput );

EDIT 2

If you want to calculate the time usage, then when opening up the question page store the time somewhere:

DateTime startTime = DateTime.Now;

Then when the question is answered, do the following:

TimeSpan questionDuration = DateTime.Now - startTime;

questionDuration will now contain the time used from opening up the question to answering it.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161831

A TimeSpan holds a duration, like 60 seconds. A DateTime holds a fixed date and time. That's what you should use.


When you say "the time" for the question, do you mean how long it should take to answer the question? If so, then you're looking for a duration, so you should use TimeSpan. If you are able to limit this to some number of minutes (with no fractions), then simply use an integer.

Upvotes: 2

Related Questions