I'm trying to create a javascript date object in the focus.add_days function to add some days to the given date in a element. the problem is that the javascript object doesn't expect a string "Y-m-d" so how can I create the date object without parsing the string "Y-m-d" into pieces, or is the only way?
trigger = {
fecha_ini: function(){
$('input[name="fecha_ini"]').on('change',function(){
console.log('hi');
var fecha_fin = $(':input [name=fecha_fin]');
var min = $(this).val();
//here is where i pass the "Y-m-d" string as the date argument
var max = fechas.add_days(min,31*4);
fecha_fin.attr('min',min);
fecha_fin.attr('max',max);
fecha_fin.val('');
})
}
};
fechas = {
add_days: function addDays(date, days) {
//here is where i use the string "Y-m-d" to create the date object, but obviusly doesnt work
var result = new Date(date);
result.setDate(date.getDate() + days);
return result;
}
};
trigger.fecha_ini();
While Date.parse will convert strings in y/m/d/ format to date objects, manual parsing is the only sensible way:
ES5 specifies a form of ISO 8601 that should be supported by all browsers, however it is not supported consistently or by all browsers in use.
With me it's working without doing anything.
I just write new Date($('#html5dateinput').val());
that's about it. I get the correct date object. I'm using Google Chrome ver 38 and I tested it on Firefox 33 under Ubuntu 14.04
Use valueAsNumber.
Use
valueAsDate
:Demo: