我试图在focus.add_days函数中创建一个javascript日期对象,以在元素中给定的日期添加一些日子。 问题是javascript对象不期望字符串“Y-m-d”,那么如何在不将字符串“Y-m-d”分解成块的情况下创建日期对象,还是唯一的方法?JavaScript的日期对象来自<input type = date>
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();
看看moment.js – hgoebl