Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Is this data annotation correct for date and time?

Is this data annotation correct for date and time?

Scheduled Pinned Locked Moved ASP.NET
asp-netcsharpdatabasedotnetquestion
7 Posts 3 Posters 48 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #1

    Hi, I'm creating models for my ASP.NET Core project. I want to use "Code First" approach to create database and tables. Please check these annotations to be correct:

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime creationDate { get; set; }
    [Required]
    [DataType(DataType.Time)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
    public DateTime creationTime { get; set; }

    And please tell me that what 0 means inside the curly braces.

    L J 2 Replies Last reply
    0
    • A Alex Dunlop

      Hi, I'm creating models for my ASP.NET Core project. I want to use "Code First" approach to create database and tables. Please check these annotations to be correct:

      [Required]
      [DataType(DataType.Date)]
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
      public DateTime creationDate { get; set; }
      [Required]
      [DataType(DataType.Time)]
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
      public DateTime creationTime { get; set; }

      And please tell me that what 0 means inside the curly braces.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The DataFormatString value is a format string to convert a date to a string in the format shown. The zero gives the position of the data parameter (following the format string) when the format is used, like:

      string theDate = string.Format(DataFormatString, someDateTimeValue);

      See String.Format Method (System) | Microsoft Docs[^]

      A 1 Reply Last reply
      0
      • L Lost User

        The DataFormatString value is a format string to convert a date to a string in the format shown. The zero gives the position of the data parameter (following the format string) when the format is used, like:

        string theDate = string.Format(DataFormatString, someDateTimeValue);

        See String.Format Method (System) | Microsoft Docs[^]

        A Offline
        A Offline
        Alex Dunlop
        wrote on last edited by
        #3

        Is data annotation for the time correct?

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]

        L 1 Reply Last reply
        0
        • A Alex Dunlop

          Is data annotation for the time correct?

          [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I deliberately did not answer that part because I don't know the answer. Does it correspond to the documentation?

          A 1 Reply Last reply
          0
          • L Lost User

            I deliberately did not answer that part because I don't know the answer. Does it correspond to the documentation?

            A Offline
            A Offline
            Alex Dunlop
            wrote on last edited by
            #5

            No, I did not find any reference for time format. Can I use String type instead?

            L 1 Reply Last reply
            0
            • A Alex Dunlop

              No, I did not find any reference for time format. Can I use String type instead?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              As I said above: DataType Enum (System.ComponentModel.DataAnnotations) | Microsoft Docs[^].

              1 Reply Last reply
              0
              • A Alex Dunlop

                Hi, I'm creating models for my ASP.NET Core project. I want to use "Code First" approach to create database and tables. Please check these annotations to be correct:

                [Required]
                [DataType(DataType.Date)]
                [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
                public DateTime creationDate { get; set; }
                [Required]
                [DataType(DataType.Time)]
                [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm:ss}")]
                public DateTime creationTime { get; set; }

                And please tell me that what 0 means inside the curly braces.

                J Offline
                J Offline
                jkirkerx
                wrote on last edited by
                #7

                I get what you want to do here, which is to create models to match your database perhaps, so each table has a matching model. But your over thinking this, and don't need that much detail. Keep it simple and flexible at first, and only add detail when needed. For some reason Microsoft likes to capitalize the first letter, and will nag you later about the first letter being lower case. This is an example model in c# to match a DB table or collection. Your example is a like a model used to present data in the view. Even when presenting data in the view, I would keep that simple as well. You want to craft models as simple as possible, and try to make them as reusable as possible to keep models down in count.

                public class PORTFOLIOS
                {
                // standard BsonId generated by MongoDb

                    public string Id { get; set; }
                    public DateTime TimeStamp { get; set; }        
                    public string Name { get; set; }
                    public string Description { get; set; }
                    public string CreatedBy { get; set; }
                    public string UpdatedBy { get; set; }
                    public DateTime UpdateDate { get; set; }
                    public string HTML { get; set; }
                    public string AvatarB64 { get; set; }
                    public string AvatarName { get; set; }
                    public string AvatarType { get; set; }
                    public string AvatarUrl { get; set; }
                    public int AvatarX { get; set; }
                    public int AvatarY { get; set; }        
                }
                

                If it ain't broke don't fix it Discover my world at jkirkerx.com

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups