32

Using an HTML input type="date" and a submit button. I would like to populate the variables day, month, and year with the appropriate values from the date input.

<input type="date" id="date-input" required />
<button id="submit">Submit</button>

and the jQuery:

var day, month, year;

$('#submit').on('click', function(){
  day = $('#date-input').getDate();
  month = $('#date-input').getMonth() + 1;
  year = $('#date-input').getFullYear();
  alert(day, month, year);
});

Here's a code sample: https://jsfiddle.net/dkxy46ha/

the console error is telling me that .getDate() is not a function.

I have seen similar questions but the solutions have not worked for me. How can I extract the day, month and year from the input type="date"? Thanks

1
  • value is string you need to create new Date from Commented Oct 23, 2015 at 22:33

5 Answers 5

49

Firstly you need to create a Date object from input element value. And then you will be able to get day, month and year from this object.

$('#submit').on('click', function(){
  var date = new Date($('#date-input').val());
  var day = date.getDate();
  var month = date.getMonth() + 1;
  var year = date.getFullYear();
  alert([day, month, year].join('/'));
});

Working example: https://jsfiddle.net/8poLtqvp/

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

2 Comments

Yeah, this doesn't work. The day if off by +/- 1. If you add 1 to it, you risk invalid dates.
Use getUTCDate() as opposed to getDate(). The same goes for month and year.
9

Date value returned by input type="date" is format yyyy-mm-dd . Could use .split() with argument "-" to retrieve array containing [yyyy, mm, dd]

Note, alert() expects string as parameter ; does not print values same as console.log() with comma , operator

var day, month, year;

$('#submit').on('click', function() {
  var date = $('#date-input').val().split("-");
    console.log(date, $('#date-input').val())
  day = date[2];
  month = date[1];
  year = date[0];
  alert(day + month + year);
});

jsfiddle https://jsfiddle.net/dkxy46ha/2/

Comments

5
date = new Date($('#date-input').val())
date.getDate()

...

2 Comments

It will not work. You need to get value of input. And only then create Date object
date = new Date($('#date-input').val())
2

The Advantage of JQuery ? A few lines of code do a lot !!

alert only takes a string as its parameter. So simply, we just have to pass our Date object as a string using th String built-in-Function.

Just replace the button tag with this...

<button onclick="alert(String( $("#date-input").val() ))">Submit</button>

You can do further manipulations on the string you'll be alerted as yyyy-mm-dd

Comments

1
        <input type="date" id="date-input"/>
        <input type="submit" id="submit" value="submit"/>


<script>
      $('#submit').on('click', function(){
              var date = new Date($('#date-input').val());
              var day = $('#date-input').getDate();
              var month = $('#date-input').getMonth() + 1;
              var year = $('#date-input').getFullYear();
              alert(day+"/"+ month+"/"+year);
            });
    </script>

Date class will convert input data into date type. Then getMonth() gives us value from 0-11 so we can either consider January as 0 or we can add 1 to received value that's what I did here.

Here I just used Jquery to fetch date, months and year respectively and if you want to post the data you can convert that values into string.

2 Comments

you should alway explain what your code does. It will be more helpful for everybody also in the future
It would be helpful to others if you can provide details on how you arrive on this solution.

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.