HTML5输入datetime-当前默认值和当前时间

时间:2014-06-28 15:55:04

标签: html5

无论如何,我可以制作HTML5输入类型的默认值=' datetime-local'到今天的日期和当前时间。

之前谢谢

6 个答案:

答案 0 :(得分:7)

这是可能的。通过使用JQuery函数,您可以拥有一个非常完整的解决方案。

这是一个例子。

JSFiddle http://jsfiddle.net/v8MNx/1/

<强> HTML

<form action="demo.html" id="myForm">
    <p>
        <label>Date:</label>
        <input type="datetime" name="anniversaire" id="anniversaire"/>
    </p>
    <input type="submit" value="Submit"/>
</form>

<强> JQuery的:

//Function found here: https://gist.github.com/ryanburnette/8803238
$.fn.setNow = function (onlyBlank) {
  var now = new Date($.now())
    , year
    , month
    , date
    , hours
    , minutes
    , seconds
    , formattedDateTime
    ;

  year = now.getFullYear();
  month = now.getMonth().toString().length === 1 ? '0' + (now.getMonth() + 1).toString() : now.getMonth() + 1;
  date = now.getDate().toString().length === 1 ? '0' + (now.getDate()).toString() : now.getDate();
  hours = now.getHours().toString().length === 1 ? '0' + now.getHours().toString() : now.getHours();
  minutes = now.getMinutes().toString().length === 1 ? '0' + now.getMinutes().toString() : now.getMinutes();
  seconds = now.getSeconds().toString().length === 1 ? '0' + now.getSeconds().toString() : now.getSeconds();

  formattedDateTime = year + '-' + month + '-' + date + 'T' + hours + ':' + minutes + ':' + seconds;

  if ( onlyBlank === true && $(this).val() ) {
    return this;
  }

  $(this).val(formattedDateTime);

  return this;
}

$(function () {
    // Handler for .ready() called.
    $('input[type="datetime"]').setNow();

});

答案 1 :(得分:5)

您可以使其更短:

select *
from (
    select s.*, row_number() over(partition by emp_no, salary order by to_date desc) rn
    from employees.salaries s
) t
where rn = 1;
    while read i; do
        echo $i
        if grep -q "$i" $document; then
            echo "$i" 'was found in' $document
        else
            echo "$i" >> $document
        fi
    done <<<$1

答案 2 :(得分:2)

对我来说,可接受的答案似乎很复杂……这是一个不需要jQuery的较短解决方案

JSFiddle: https://jsfiddle.net/rzaceg8v/

window.addEventListener("load", function() {
    var now = new Date();
    var utcString = now.toISOString().substring(0,19);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    var localDatetime = year + "-" +
                      (month < 10 ? "0" + month.toString() : month) + "-" +
                      (day < 10 ? "0" + day.toString() : day) + "T" +
                      (hour < 10 ? "0" + hour.toString() : hour) + ":" +
                      (minute < 10 ? "0" + minute.toString() : minute) +
                      utcString.substring(16,19);
    var datetimeField = document.getElementById("myDatetimeField");
    datetimeField.value = localDatetime;
});
<input type="datetime-local" id="myDatetimeField"/>

答案 3 :(得分:1)

这完美无缺!

一个注意事项,我在10月份尝试了这个唯一的月份。它会给我一个'010',而不是10。

month =(now.getMonth() +1 )。toString()。length === 1? '0'+(now.getMonth()+ 1).toString():now.getMonth()+ 1;

答案 4 :(得分:0)

大行对我有用,如果不行,您可以在表达式中引入空格和换行符。

function setDatetimeInput(element, t = new Date()){
    function p(number){return number.toString().padStart(2, '0');}//number to 2 digit, 0 padded string
    element.value = `${t.getFullYear()}-${p(t.getMonth()+1)}-${p(t.getDate())}T${p(t.getHours())}:${p(t.getMinutes())}`;
}

答案 5 :(得分:0)

以上方法有效,但对我来说太冗长了。 这是我的版本:

window.addEventListener("load", function() {
    var now = new Date();
    var offset = now.getTimezoneOffset() * 60000;
    var adjustedDate = new Date(now.getTime() - offset);
    var formattedDate = adjustedDate.toISOString().substring(0,16); // For minute precision
    var datetimeField = document.getElementById("myDatetimeField");
    datetimeField.value = formattedDate;
});