在jQuery中如何在类型="date"时设置日期 [英] how to set date while type ="date".in jquery

查看:700
本文介绍了在jQuery中如何在类型="date"时设置日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置日期.我使用了.val()函数,但在android中不起作用.

I want to set date .I used .val() function but it not work in android.

<input name="caseDate" id="caseDate"  min="2000-01-01" value="" type="date"  class="caseDate_h" >

JS:

 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) ;
 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) ;
 $('#caseDate').val(today); 
 $("#caseDate").attr("value", today);

推荐答案

您必须将日期解析为"yyyy-mm-dd"格式

You have to parse the date into the format of "yyyy-mm-dd"

var todaysDate  = new Date();
var month = parseInt(todaysDate.getMonth() / 10) <= 0 ? "0" + todaysDate.getMonth() : todaysDate.getMonth();
var date = parseInt(todaysDate.getDate() / 10) <= 0 ? "0" + todaysDate.getDate() : todaysDate.getDate();
console.log(month);
console.log(date)
var today = todaysDate.getFullYear() + "-" + month + "-" + date;
console.log(today);
$('#caseDate').val(today);

这应该有效.

这篇关于在jQuery中如何在类型="date"时设置日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆