Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form to get StartDate and EndDate.

C#
[Display(Name = "StartDate")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0: MM/dd/yyyy}",
            ApplyFormatInEditMode =true)]
        public System.DateTime StartDate { get; set; }


input is like this :

HTML
<pre><div class="form-group">
                        <label>End Date  </label>
                        <input  asp-for="Slider.EndDate"  name="EnDate"  id="enDate" required />
                    </div>


in code behind there is a property named Slide :

C#
<pre>  [BindProperty]
        public Carousel.DataLayer.Entities.Slider.Slider Slider { get; set; }


the value of comes to action method is : 1/1/0001 and it saves in sql like this

What I have tried:

I changed the format in class Slider several times and I added datepicker .
Posted
Updated 6-Oct-21 11:56am
v3
Comments
PIEBALDconsult 6-Oct-21 15:46pm    
Dates/times should not be stored as text, therefore not with any format.

DateTime values (in both C# and SQL) aren't stored with a format - they are held as a number of "Ticks" since a defined moment in time.
The only way to hold a format with a date is to store it as a string, and that's a really poor idea (especially with a web based app) because the date format that users enter and expect to see will be different from user to user: the US uses MM/dd/yy, Europe uses dd/MM/yy, and Japan / developers use ISO format yy/MM/dd.
Storing it as a string is bad because at some point the wrong date format will be used and the whole of your DB data becomes suspect, or your queries fail as a result.

Forget date formats: validate and convert user input as early as possible (while you still have access to the user preferences), store it as a "proper" DateTime value, and only convert it back to a formatted string at the last moment when again you have access to teh new user preferences.

Doing at any other way is just building up problems for yourself, and your users. And some of them can take some serious sorting out once the DB starts to get corrupted "bad format" values.
 
Share this answer
 
Comments
ali mahdian 6-Oct-21 16:39pm    
Thanks for your answer . I removed type="date" .The format is MM/dd/yyyy .Should I add some thing else ? (by the way I updated my question with more infomation)
HTML
<pre> <div class="form-group">
                        <label>End Date  </label>
                        <input  asp-for="Slider.EndDate" name="EndDate"  id="enDate" required />
                    </div>


I set attribute name for input ,while I'm using asp-for .I removed attribute name and now it works correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900