19

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

2
  • You should clarify what you wish to set as the value of the control. The format for it, in the value attribute, is fixed. Is your question about converting some date value from some format (which?) to that format? Commented May 27, 2013 at 11:22
  • as wachk said input type date is 'YYYY-MM-DD', initially i wanted to change that format to 'DD-MM-YYYY'. and if we can open the default calendar(date-picker) of mobile device with JS then i can skip using input type date Commented May 27, 2013 at 11:37

12 Answers 12

21

The format is YYYY-MM-DD. You cannot change it.

$('#myinput').val('2013-12-31'); sets value

Sign up to request clarification or add additional context in comments.

5 Comments

on all browsers will it be YYYY-MM-DD ? in case of mobile safari and android ?
the internal value may be read and written as YYYY-MM-DD but that isn't necessarily how it will be presented -- the browser will present the value how it decides to, which in the case of mobile devices will look at the user's locale settings and use that format
my format is set to dd-mm-yyyy ? whats up with this?
The format is set according to locale of your PC. I am in canada, for me the format of this field is mm/dd/yyyy
yes, it change according the language and region of the computer
19

new Date().toISOString().split('T')[0];

4 Comments

For now is the most beautiful and neat solution.
new Date().toISOString().substring(0,10) is what I use - dunno what's faster to read.
hm, this works although returns date in YYYY-MM-DD unlike the value of such input (YYYY-MM-DD).. is this a cross-browser locale-independent solution?
the problem with this solution is that toISOString always presents the timezone in zero UTC offset, thus be aware of that
6

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()); 
});

Comments

3

Canadian locale happens to follow the same format, too:

new Date().toLocaleDateString('en-CA')

Comments

2

Easier than the above is

var today = new Date().toISOString().substring(0,10); # "2013-12-31"

Comments

1

It's ugly, but it works. :/

var today = new Date().toLocaleString('en-GB').split(' ')[0].split('/').reverse().join('-');

1 Comment

That would give you date strings like "2015,-05-01". To remove the unnecessary comma, try this new Date().toLocaleString('en-GB').split(' ')[0].split('/').reverse().join('-').replace(',','');
0

Please check this https://stackoverflow.com/a/9519493/1074944 and try this way also $('input[type="date"]').datepicker().prop('type','text'); check the demo

Comments

0

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                                                          
        });                                                                          
      });                                                                                

Comments

0
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.

Comments

0

@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

Comments

0

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);

Comments

0

Here is a simple answer,

Since this is a string, we can use Javascript String slice method to rearrange the characters

<input type="date" id="idate" name="tno"><br>
<button onclick="run()">Run</button>

<p id="demo"></p>

<script>
function run() {
var d = document.getElementById("idate").value;

document.getElementById("demo").innerHTML = d.slice(8, 10) + "/" + d.slice(5, 7) + "/" + d.slice(0, 4);
}
</script>

Source

https://www.w3schools.com/jsref/jsref_slice_string.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.