将日期输入类型默认值设置为今天,明天,Anydate?

时间:2014-01-25 22:12:54

标签: jquery html5 date input

在HTML5中,没有在value属性中指定“today”的本机方式。这是我非常喜欢的jQuery代码。如何扩展此代码以设置

  • 今天的日期var today
  • 明天的日期var tomorrow
  • 计算为var anydate的任何日期(从var today计算/启动?)

并相应地定义以下3个id-s:

  • #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);
});

demo

4 个答案:

答案 0 :(得分:11)

  

与任何HTML输入字段一样,除非使用value属性指定默认值,否则浏览器将为空。

     

不幸的是,HTML5没有提供在value属性(我可以看到)中指定“今天”的方法,只提供2011-09-29有效日期,如+1

     

来源:RFC3339

在这种情况下,您可以编写一个脚本只需input id来查找明天的日期,但您首先必须为<input type="date" id="theDate"> 添加默认值今天的日期

至于 anydate ?不完全确定你的意思。就像一个约会者?

问题有点不清楚,但我想我会尽可能多地帮助提供信息。


要通过jQuery分配日期,你总是可以这样做......

Tak's answer on "HTML5 Input Type Date — Default Value to Today?"

<强> HTML:

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'));

<强> jQuery的:

<input type="date" id="theDate">
<input type="date" id="tomorrowDate">

编辑:

此外,要查找今天的日期和明天的日期,但确保月末或年末不会影响它,请改用:

http://jsfiddle.net/SinisterSystems/4XkVE/4/

<强> HTML:

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);

<强>的jQuery

{{1}}

答案 1 :(得分:4)

根据Nicholas Hazel的回复和user113716关于leading zeroes for dates的回答,这里有一个简明的函数,可以将日期格式化为YYYY-MM-DD并设置“日期”的值“输入控制类型。

http://jsfiddle.net/wloescher/2t8v7fnf/2/

<强> 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

// 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);
}

答案 2 :(得分:1)

我找到了非常简单的解决方案:

echo (new DateTime('tomorrow'))->format('Y-m-d'); //-- For tomorrow
echo (new DateTime())->format('Y-m-d'); //-- For today

答案 3 :(得分:0)

使用toOSOString(),它总是返回一个“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);