layui 时间选择 怎么给后端传时间戳

提问 已结 5 336
HH丶
HH丶 2019-8-14
悬赏:20飞吻
版本:layuiAdmin 浏览器:chrome
回帖
  • var date = new Date('2014-04-23 18:55:49:123');
    // 有三种方式获取
    var time1 = date.getTime();
    var time2 = date.valueOf();
    var time3 = Date.parse(date);
    console.log(time1);//1398250549123
    console.log(time2);//1398250549123
    console.log(time3);//1398250549000
    0 回复
  • 传日期给后台,让后台处理会好些。

    非要前端的话封装个函数
    function strtotime(string) {
    var f = string.split(' ', 2);
    var d = (f[0] ? f[0] : '').split('-', 3);
    var t = (f[1] ? f[1] : '').split(':', 3);
    return (new Date(
    parseInt(d[0], 10) || null,
    (parseInt(d[1], 10) || 1) - 1,
    parseInt(d[2], 10) || null,
    parseInt(t[0], 10) || null,
    parseInt(t[1], 10) || null,
    parseInt(t[2], 10) || null
    )).getTime() / 1000;
    }
    0 回复
  • IDyun
    2019-8-14


    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css" media="all">
    <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
    </head>
    <body>

    <form class="layui-form" action="">

    <div class="layui-inline">
    <label class="layui-form-label">日期</label>
    <div class="layui-input-inline">
    <input type="text" name="date" id="date" lay-verify="timestamp" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
    </div>
    </div>
    <div class="layui-form-item">
    <div class="layui-input-block">
    <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
    </div>
    </form>

    <script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
    <!-- 注意:如果你直接复制所有代码到本地,上述js路径需要改成你本地的 -->
    <script>
    layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
    ,layer = layui.layer
    ,laydate = layui.laydate;

    //日期
    laydate.render({
    elem: '#date'
    });

    //自定义验证规则
    form.verify({
    timestamp: function(value,elem){
    //转换时间戳
    elem.value = new Date(value).getTime();
    }
    });


    //监听提交
    form.on('submit(demo1)', function(data){
    layer.alert(JSON.stringify(data.field), {
    title: '最终的提交信息'
    })
    return false;
    });

    });
    </script>

    </body>
    </html>
    0 回复
  • 日期按照格式作为字符串传给后台 后台在进行格式转换
    0 回复
  • 忘卿
    2019-8-15
    js转换的时间戳如果发现最后有000 记得除1000
    0 回复