在输入类型日期中设置日期 [英] Set date in input type date

查看:16
本文介绍了在输入类型日期中设置日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在 Chrome 中的 datepicker 输入 type date 中设置今天的日期.

I will set today's date in the datepicker input type date in Chrome.

$(document).ready(function() {
    let now = new Date();
    let today = now.getDate()  + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
    console.log(today);
    $('#datePicker').val(today);
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="datePicker" type="date">

但是,它不起作用.

请在 Chrome 中尝试代码片段.

Please try the code snippet in Chrome.

推荐答案

Fiddle 链接:http://jsfiddle.net/7LXPq/93/

这里有两个问题:

  1. HTML 5 中的日期控件接受我们在 SQL 中使用的年-月-日格式
  2. 如果月份是9,则需要将其设置为09,而不是简单地设置为9.所以它也适用于日场.

请按照演示的小提琴链接:

Please follow the fiddle link for demo:

var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day) ;

$('#datePicker').val(today);

这篇关于在输入类型日期中设置日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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