1

I'm learning, and still getting used to, asp.net-mvc, and C#, and the 'T' version of SQL.

I have a database where I made one field DueDate, data type Date... and another field DueTime, data type Time in the .sql file to create the table and seed it with some example entries.

But in my model.cs, in C#, it seems there is only the DateTime data type in C#?

But using that data type makes it so that in the View, you have to enter a date and a time for the DueTime field, which is neither what I want, nor is it convenient or palatable for users.

I have searched the documentation, and googled for examples, and I can't seem to find any example similar enough to what I'm doing, that I could extrapolate something.

Here is the .sql file to create & seed the table:

CREATE TABLE [dbo].[Homeworks]      /* use plural db name */
(
    [ID] INT IDENTITY (1,1) NOT NULL,
    [Priority]  NVARCHAR(18)        NOT NULL,
    [DueDate]   DATE        NOT NULL,
    [DueTime]   TIME(0)     NOT NULL,
    [Dept]      NVARCHAR(4) NOT NULL,
    [Course]    NVARCHAR(9) NOT NULL,
    [Assignment]    NVARCHAR(64)    NOT NULL,
    [Notes]     NTEXT       NOT NULL,

    CONSTRAINT [PK_dbo.Homeworks] PRIMARY KEY CLUSTERED ([ID] ASC)
);

INSERT INTO [dbo].[Homeworks] (Priority, DueDate, DueTime, Dept, Course, Assignment, Notes) VALUES
    ('super-important','2019-11-11','11:59:00', 'CS', '123', 'Questions/Answers', 'best to read ch 5 first'),
    ('meh','2019-11-18','11:59:00','CS','234','write helloWorld program','find out how to write a helloWorld program'),
    ('important','2019-11-13','11:59:00','MTH','123','chapter 3 problems','do the odd-numbered ones'),
    ('regular','2019-11-15','11:59:00','WR','101','write essay paper','figure out something to write an essay about'),
    ('notes','2019-11-14','11:59:00','CS','189','do up notes over chapters 1-5','make sure you know that crap for the mid-term')

GO

Here is the relevant section of the model class:

        [Required]
        [DisplayName("Due Date")]
        public DateTime DueDate { get; set; }

        //------------------------------------------------------------
        [DisplayName("Due Time"), Required]
        public DateTime DueTime { get; set; }

It all works, as in being functional when you run the page on localhost -- as long as you enter the full date in the time field. But input goes into the database as it should.

How do I get the form to accept simply the time, in the time field? What data type to I use?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Stormy
  • 172
  • 1
  • 1
  • 9
  • @aage Actually [this](https://stackoverflow.com/questions/1026841/how-to-get-only-time-from-date-time-c-sharp) is closer -- but still, I tried following that example, and get errors all over..... I'm just not sure how to pull it all together -- there's gotta be a way! – Stormy Nov 02 '19 at 13:05
  • Maybe something like [this](https://stackoverflow.com/a/17565435/5803406) would work? Essentially requiring the model to be submitted in a time format – devNull Nov 02 '19 at 14:13

1 Answers1

3

The .Net framework does not have a built in Time type nor does it have a built in Date type.
It does have TimeSpan, but that is not the same as Time - A c# TimeSpan is actually a duration - the amount of time between two points in time - while SQL Server's Time data type is actually the time of day - and is also documented as such:

Defines a time of a day. The time is without time zone awareness and is based on a 24-hour clock.

It's range is 00:00:00.0000000 through 23:59:59.9999999.

The .Net's TimeSpan struct, however, is documented as

Represents a time interval.

and it's range spans from it's MinValue through has MaxValue fields, which are

-10675199.02:48:05.4775808 through 10675199.02:48:05.4775807.

Having said all that, since the .Net framework has no TimeOfDay data type, it use a TimeSpan to map to SQL Server's Time data type, and you can feel free to use that data type to represent the time of day (that's also the type of the DateTime's struct Time property, which means that the .Net framework itself use this type to represent time of day.

If you want an alternative date time api for .Net, you should check out NodaTime.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121