Input of type date in jQuery

0

I'm looking for a simple way to get the separated day, month, and year values from a input of type date , in jQuery.

$( "#ano" ).html($( "input[type=date]" ).val(  ) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="date" value="2018-10-17">
<div id="ano"></div>

How do I get the separated values without using split() ?

    
asked by anonymous 17.10.2018 / 06:07

1 answer

0

jQuery itself, as far as I know, has no method for this. What you can do is to convert the value of the field to the Date() object of JavaScript and get the values. Simply replace the hyphens with commas and get the values.

  

The string should be   a format recognized by the Date.parse() ( IETF-compliant RFC   2822 timestamps and also a ISO8601 version ).

var input = $("input[type=date]").val().replace(/-/g, ",");
var data = new Date(input);
$( "#ano" ).html( data.getFullYear() );
$( "#mes" ).html( data.getMonth()+1 );
$( "#dia" ).html( data.getDate() );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="date" value="2018-10-17">
<div id="ano"></div>
<div id="mes"></div>
<div id="dia"></div>
  

+1 month value added because JavaScript counts the   months from 0 : January = 0, February = 1 etc.

    
17.10.2018 / 06:26