Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have input htm control

<input  type="date"> 


when iam accessing throgh google chrome date is display in
M/dd/yyyy


in IE it is display yyy-mm-dd

how to make in all browsers same format
M/dd/yyyy


What I have tried:

tried by keeping place holder for input control as
M/dd/yyyy
Posted
Updated 25-Mar-20 7:42am

If you want a specific format, use three controls: Year (when selected, enables Month), Month (when selected, enables Day and fills it). Use dropdown lists and the user can't get the format wrong, and it'll work the same in all browsers.
 
Share this answer
 
Comments
Chinnu2020 24-Mar-20 10:16am    
I am assigning from back end to input control
Dave Kreskowiak 24-Mar-20 10:20am    
and it seems you completely ignored what 'Griff wrote in his answer.

Do it with 3 drop down lists and you don't have to do any formatting of the input whatsoever.
Chinnu2020 24-Mar-20 10:24am    
There is already data in ng-model while assinging to input control , and its read only and there is no option to select date for this
gggustafson 25-Mar-20 13:16pm    
Instead of directly assigning the "data in ng-model" to the input control; parse it into three components (year, month, day) and assign each component to its appropriate dropdown.
Browsers which support date inputs will use the user's preferred date format for displaying and entering the date. There is no way to override that, since it would be confusing for the user.

The value of the date input will always use the yyyy-MM-dd format, to ensure that the value is unambiguous.

Browsers which don't support date inputs - and that includes all versions of IE - will display a simple text input containing the raw value. There will be no validation, no localised formatting, and no date picker. You will need to use a custom Javascript date picker control in those browsers if you want to provide a user-friendly way to select a date.

The script to detect whether the browser supports date inputs is fairly simple - this StackOverflow answer[^] has a good example:
JavaScript
function checkDateInput() {
    var input = document.createElement('input');
    input.setAttribute('type','date');

    var notADateValue = 'not-a-date';
    input.setAttribute('value', notADateValue); 

    return (input.value !== notADateValue);
}
If that function returns false, you'll need to use a Javascript date picker control.

Can I use... Date and time input types[^]
 
Share this answer
 
Comments
Chinnu2020 26-Mar-20 15:26pm    
how to keep validation for date it should accept only m/dd/yyyy, presently when i enter some data and click on save button its allowing but its not saving in database.
Richard Deeming 26-Mar-20 15:37pm    
A date input will always submit using the yyyy-MM-dd format.

If you're using MVC's model binding to bind to a DateTime property, that should just work. If you're manually parsing the submitted data, you'll need to use DateTime.TryParseExact[^] to parse it using that format.

Once you've got a DateTime, then you don't need to format it to store it in the database. You set the parameter value to the DateTime, and store that parameter value in a date column.

If you're having problems passing the DateTime to the database, then either you're not using a parameterized query, or you're using the wrong data type on your column.
Chinnu2020 27-Mar-20 8:03am    
just i need to show pop if user enters in correct date format in textbox.
Richard Deeming 27-Mar-20 8:09am    
There is no such thing as an "incorrect date format". Users will enter the date in the format determined by their locale. The browser will send the date to the server in the unambiguous yyyy-MM-dd format.

If you're trying to force the users to enter a date in a particular format, then you're doing it wrong.
Chinnu2020 27-Mar-20 10:57am    
function dateValidate(inputField) {
var datePat = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
var matchArray = String(inputField.value).match(datePat);
if (matchArray === null) {
alert("Start Date must be in MM/DD/YYYY format");
return false;
}
}

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