原生js 写 日历

这篇博客介绍了如何在HTML5中使用原生的日期和时间输入控件,并通过JavaScript实现获取当前日期和时间的功能。示例代码展示了如何设置和获取日期及时间输入框的值,并提供了点击按钮获取当前时间的交互操作。
摘要由CSDN通过智能技术生成

引用: https://blog.csdn.net/djk8888/article/details/79727826


<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>html5原生自带的日期控件和时间控件</title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(function () {
            NOW();
        });
        //初始化时间控件
        function NOW() {
            var now = new Date();
            var nowYear = now.getFullYear(); //年
            var nowMonth = now.getMonth() + 1 < 10 ? "0" + (now.getMonth() + 1) : now.getMonth(); //月
            var nowDay = now.getDate() < 10 ? "0" + now.getDate() : now.getDate(); //日期
            var nowHour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours(); //时
            var nowMinute = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes(); //分
            var nowDate = nowYear + "-" + nowMonth + "-" + nowDay;
            var nowTime = nowHour + ":" + nowMinute;
 
            $("#nowDate").val(nowDate);
            $("#nowTime").val(nowTime);
        }
 
        $(function () {
            $("#btnNow").click(function () {
                NOW();
            });
        });
    </script>
</head>
<body>
    <div>
        <input type="date" id="nowDate" />
        <input type="time" id="nowTime" />
        <input type="button" id="btn" value="获取时间" />
        <input type="button" id="btnNow" value="当前时间" />
    </div>
</body>
</html>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            var nDate = $("#nowDate").val();
            var nTime = $("#nowTime").val();
            alert(nDate + " " + nTime);
        }); 
    });
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值