Is there any way to set the format of <input type="date" />
? if no then how can i set date in this field using JavaScript in the default format of type="date"
. how can i get what will be the format of this field?
EDIT : Actually i want to show native date-picker of mobile device that's why i picked this type of input. if is there any alternate for that field that also will b good to have.
Sorry if i said anything stupid. Please guide me
This question is related to
javascript
jquery
html
mobile-safari
try this :)
function getDefaultDate(){
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
return today;
}
$(document).ready(function(){
$("#dateid").val( getDefaultDate());
});
function getDefaultDate(curDate){
var dt = new Date(curDate);`enter code here`
var date = dt.getDate();
var month = dt.getMonth();
var year = dt.getFullYear();
if (month.toString().length == 1) {
month = "0" + month
}
if (date.toString().length == 1) {
date = "0" + date
}
return year.toString() + "-" + month.toString() + "-" + date.toString();
}
In function pass your date string.
Easier than the above is
var today = new Date().toISOString().substring(0,10); # "2013-12-31"
@cOnstructOr provided a great idea, but it left a comma in place
var today = new Date().toLocaleString('en-GB').split(' ')[0].slice(0,-1).split('/').reverse().join('-');
fixes that
Please check this https://stackoverflow.com/a/9519493/1074944 and try this way also $('input[type="date"]').datepicker().prop('type','text');
check the demo
What you want to do is fetch the value from the input and assign it to a new Date instance.
let date = document.getElementById('dateInput');
let formattedDate = new Date(date.value);
console.log(formattedDate);
It's ugly, but it works. :/
var today = new Date().toLocaleString('en-GB').split(' ')[0].split('/').reverse().join('-');
I think this can help
function myFormatDateFunction(date, format) {
...
}
jQuery('input[type="date"]')
.each(function(){
Object.defineProperty(this,'value',{
get: function() {
return myFormatDateFunction(this.valueAsDate, 'dd.mm.yyyy');
},
configurable: true,
enumerable : true
});
});
Canadian locale happens to follow the same format, too:
new Date().toLocaleDateString('en-CA')
new Date().toISOString().split('T')[0];
Source: Stackoverflow.com