美文网首页
Layui时间控件回调获取时间戳

Layui时间控件回调获取时间戳

作者: 村东头老骥 | 来源:发表于2019-10-09 16:55 被阅读0次

Layui时间控件回调获取时间戳

效果展示

code

<script>


    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#test1' //指定元素
            , type: 'datetime'
            , range: true
            , done: function (value, date, endDate) {

                console.log(value); //得到日期生成的值,如:2017-08-18
                console.log(date); //得到日期时间对象:{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
                console.log(endDate); //得结束的日期时间对象,开启范围选择(range: true)才会返回。对象成员同上。

                var start_date,
                    end_date,
                    start_date_timestamp,
                    end_date_timestamp;
                // 初始化时间日期对象
                start_date = new Date(date.year, date.month, date.date, date.hours, date.minutes, date.seconds);
                end_date = new Date(endDate.year, endDate.month, endDate.date, endDate.hours, endDate.minutes, endDate.seconds);
                // 获取时间戳 -- 13 位
                start_date_timestamp = start_date.getTime() / 1000;
                end_date_timestamp = end_date.getTime() / 1000;
                // 打印时间戳
                console.log(start_date_timestamp, end_date_timestamp)

            }

        });
    });
</script>

打印结果

输出结果

补充

控件初始打开的回调

  ,ready: function(date){
    console.log(date); //得到初始的日期时间对象:{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
  }
日期时间被切换后的回调
  ,change: function(value, date, endDate){
    console.log(value); //得到日期生成的值,如:2017-08-18
    console.log(date); //得到日期时间对象:{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
    console.log(endDate); //得结束的日期时间对象,开启范围选择(range: true)才会返回。对象成员同上。
  }
初始化日期
  new Date() // 当前日期和时间
  new Date(milliseconds) //返回从 1970 年 1 月 1 日至今的毫秒数
  new Date(dateString)
  new Date(year, month, day, hours, minutes, seconds, milliseconds)
获取时间戳
  var timestamp1 = (new Date()).valueOf(); // 结果1535374762785,通过`valueOf()`函数返回指定对象的原始值获得准确的时间戳值;
  var timestamp2 = new Date().getTime();  // 结果:1535374762785,通过原型方法直接获得当前时间的毫秒值,准确;
  var timetamp3 = Number(new Date()) ; //结果:1535374762785,将时间转化为一个number类型的数值,即时间戳;

相关文章

网友评论

      本文标题:Layui时间控件回调获取时间戳

      本文链接:https://www.haomeiwen.com/subject/mnmgpctx.html