HTML5输入datetime-今天和当前时间的本地默认值 [英] HTML5 Input datetime-local default value of today and current time

查看:63
本文介绍了HTML5输入datetime-今天和当前时间的本地默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,我可以将HTML5输入类型的默认值设置为今天的日期和当前时间.

Is there anyway that I can make a default value of HTML5 input type='datetime-local' to today's date and this current time.

谢谢

推荐答案

有可能.通过使用JQuery函数,您可以获得真正完整的解决方案.

It's possible. By using a JQuery function, you can have a really complete solution.

这里是一个例子.

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

});

这篇关于HTML5输入datetime-今天和当前时间的本地默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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