jQuery 将日期输入类型默认值设置为今天、明天、任何日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21356919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set date input type default value to Today, Tomorrow, Anydate?
提问by kirandulo
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
在 HTML5 中,没有在 value 属性中指定“今天”的本地方式。这是我非常喜欢的jQuery代码。如何扩展此代码以设置
- today's date to
var today
- tomorrow's date to
var tomorrow
- any date calculated to
var anydate
(calculated/initiated fromvar today
?)
- 今天的日期到
var today
- 明天的日期
var tomorrow
- 计算到的任何日期
var anydate
(从var today
?计算/开始)
and define the following 3 id-s accordingly:
并相应地定义以下 3 个 id-s:
#theDate
#theTomorrow
#theAnydate
#theDate
#theTomorrow
#theAnydate
HTML
HTML
<input type="date" id="theDate">
jQuery
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);
});
回答by Nicholas Hazel
Like any HTML input field, the browser will leave it empty unless a default value is specified with the value attribute.
Unfortunately HTML5 doesn't provide a way of specifying 'today' in the
value
attribute (that I can see), only a RFC3339valid date like2011-09-29
.source: Tak's answer on "HTML5 Input Type Date — Default Value to Today?"
与任何 HTML 输入字段一样,除非使用 value 属性指定默认值,否则浏览器会将其留空。
不幸的是,HTML5 没有提供在
value
属性中指定“今天”的方法(我可以看到),只有一个RFC3339有效日期,如2011-09-29
.
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.
在这种情况下,您可能会编写一个脚本来简单+1
地查找明天的日期,但您首先必须input id
为今天的日期添加一个默认值。
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.
这个问题有点不清楚,但我想我会尽可能多地提供所提供的信息。
To assign a date via jQuery, you could always do something like this...
要通过 jQuery 分配日期,你总是可以做这样的事情......
http://jsfiddle.net/SinisterSystems/4XkVE/4/
http://jsfiddle.net/SinisterSystems/4XkVE/4/
HTML:
HTML:
<input type="date" id="theDate">
jQuery:
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'));
EDIT:
编辑:
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:
此外,要查找今天的日期和明天的日期,但要确保月末或年末不会影响它,请改用:
http://jsfiddle.net/SinisterSystems/4XkVE/6/
http://jsfiddle.net/SinisterSystems/4XkVE/6/
HTML:
HTML:
<input type="date" id="theDate">
<input type="date" id="tomorrowDate">
jQuery
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);
回答by wloescher
Building on Nicholas Hazel's response and user113716answer about leading zeroes for dates, here's a concise function to get a date formmated as YYYY-MM-DD and set the value of a "date" type input control.
基于Nicholas Hazel的响应和user113716关于日期前导零的回答,这里有一个简洁的函数来获取格式为 YYYY-MM-DD 的日期并设置“日期”类型输入控件的值。
http://jsfiddle.net/wloescher/2t8v7fnf/2/
http://jsfiddle.net/wloescher/2t8v7fnf/2/
HTML
HTML
<div>Today:
<input type="date" id="theDate" />
</div>
<div>Tomorrow:
<input type="date" id="theTomorrow" />
</div>
<div>Any Date:
<input type="date" id="theAnyDate" />
</div>
JavaScript
JavaScript
// Set values
$("#theDate").val(getFormattedDate(today()));
$("#theTomorrow").val(getFormattedDate(tomorrow()));
$("#theAnyDate").val(getFormattedDate(new Date("4/1/12")));
function today() {
return new Date();
}
function tomorrow() {
return today().getTime() + 24 * 60 * 60 * 1000;
}
// Get formatted date YYYY-MM-DD
function getFormattedDate(date) {
return date.getFullYear()
+ "-"
+ ("0" + (date.getMonth() + 1)).slice(-2)
+ "-"
+ ("0" + date.getDate()).slice(-2);
}
回答by Umesh Gaire
I found Very Easy Solution:
我找到了非常简单的解决方案:
echo (new DateTime('tomorrow'))->format('Y-m-d'); //-- For tomorrow
echo (new DateTime())->format('Y-m-d'); //-- For today
回答by Samuel Danielson
Use toISOString(), which always returns a "Z" timezone ISO8601 string, and truncate it to just the date.
使用 toISOString(),它总是返回一个“Z”时区 ISO8601 字符串,并将其截断为日期。
var todayUTC = new Date().toISOString().substr(0,10);
var todayLocal = new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60 * 1000).toISOString().substr(0,10);
var tomorrowLocal = new Date(new Date().getTime() + 24 * 60 * 60 * 1000 - new Date().getTimezoneOffset() * 60 * 1000).toISOString().substr(0,10);