设置日期输入类型默认值为今天,明天,Anydate? [英] Set date input type default value to Today, Tomorrow, Anydate?
问题描述
在HTML5中,没有在值属性中指定today的本机方式。这是我非常喜欢的jQuery代码。如何扩展此代码设置
In HTML5 there is not a native way of specifying 'today' in the value attribute. Here is the jQuery code I like very much. How to extend this code to set
- 今天的日期为
var today
- 明天的日期为
var tomorrow
- 计算为
的任何日期var anydate
(从var today开始计算/发起
?)
- today's date to
var today
- tomorrow's date to
var tomorrow
- any date calculated to
var anydate
(calculated/initiated fromvar today
?)
并相应地定义以下3个id:
and define the following 3 id-s accordingly:
-
#theDate
-
#theTomorrow
-
#theAnydate
#theDate
#theTomorrow
#theAnydate
HTML
<input type="date" id="theDate">
jQuery
$(document).ready(function() {
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + "-" + month + "-" + day;
$("#theDate").attr("value", today);
});
推荐答案
像任何HTML输入字段,浏览器将保留为空,除非使用value属性指定了默认值。
Like any HTML input field, the browser will leave it empty unless a default value is specified with the value attribute.
不幸的是HTML5不提供一种在 值
属性(我可以看到),只有一个 RFC3339 有效日期,如 2011-09-29
。
Unfortunately HTML5 doesn't provide a way of specifying 'today' in the value
attribute (that I can see), only a RFC3339 valid date like 2011-09-29
.
source: Tak's answer on "HTML5 Input Type Date — Default Value to Today?"
在这种情况下,您可以写一个脚本,简单地 +1
来查找明天的日期,但您首先必须为您的输入ID添加默认值
今天的日期。
In that instance, you could potentially write a script to simply +1
to find tomorrow's date, but you would first have to add a default value to your input id
for today's date.
至于 anydate ?不完全确定你的意思。像一个datepicker?
As far as anydate? Not entirely sure what you mean there. Like a datepicker?
这个问题有点不清楚,但是我想我会尽可能帮助我提供的信息。
The question was a bit unclear, but I figured I'd help as much as I could with the info provided.
要通过jQuery分配日期,您可以随时执行此操作...
To assign a date via jQuery, you could always do something like this...
HTML:
<input type="date" id="theDate">
jQuery:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'/'+dd+'/'+yyyy;
$('#theDate').attr('value', today);
alert($('#theDate').attr('value'));
编辑:
此外,要查找今天的日期和明天的日期,但确保年底或年底不会影响,请改用:
Furthermore, to find both of today's date, and tomorrow's date, but ensure the end of the month or end of the year won't affect it, use this instead:
HTML:
<input type="date" id="theDate">
<input type="date" id="tomorrowDate">
jQuery
var today = new Date();
var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var tomday = tomorrow.getDate();
var tommonth = tomorrow.getMonth() + 1;
var tomyear = tomorrow.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'/'+dd+'/'+yyyy;
if(tomday<10){tomday='0'+tomday} if(tommonth<10){tommonth='0'+tommonth} tomorrow = tommonth+'/'+tomday+'/'+tomyear;
$('#theDate').attr('value', today);
$('#tomorrowDate').attr('value', tomorrow);
这篇关于设置日期输入类型默认值为今天,明天,Anydate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!