【layui】laydata 自定义日期之外格式的处理办法

需求:点击日期组件,选择之后,显示 3月份的第三周, 查资料发现,只支持 年月日时分秒,不支持周,随想了以下解决办法

代码:

<html>
<head>
    <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" />
  <script src="https://www.layuicdn.com/layui-v2.5.6/layui.js" charset="utf-8"></script>
  <script src="https://www.layuicdn.com/layer-v3.1.1/layer.js" charset="utf-8"></script>

</head>

<style>
    
 .laofan{
    background:#009688 !important;
   
    color:#009688 !important;
    width:20px !important;
    overflow: hidden;
 }
</style>
<body> 
<div style="padding: 20px; background-color: #F2F2F2;">
    <form class="layui-form" action="">
     <input type="text" name="mytime"  class="layui-input" id="laofandata">
     <button type="button" class="layui-btn laofan" id="laofan"   ></button>
    </form>
</div>
 
<script>
    layui.use(['laydate','jquery'], function(){
        var laydate  = layui.laydate,
        $     = layui.jquery;
       
        //时间处理
        layui.use(['laydate'], function(){
            var layer = layui.layer;
            var laydate = layui.laydate;
            var util = layui.util;
            //时间选择器
            
            laydate.render({
                elem: '#laofan',
                type: 'date',
                format: 'yyyy-mm-dd',
                done: function(value, date, endDate){
                    var result = getMonthWeek(date.year,date.month,date.date);
                    $("#laofandata").val(result);
                }
            });
        });

        
        function getMonthWeek(a,b,c){
            var date = new Date(a, parseInt(b) - 1, c),
                w = date.getDay(),
                d = date.getDate();
            if(w==0){
                w=7;
            }
            var config={
                getMonth:date.getMonth()+1,
                getYear:date.getFullYear(),
                getWeek:Math.ceil((d + 6 - w) / 7),
            }
            var str=b+'月份的第'+config.getWeek+'周';
            return str; 
        }

      
    });
</script>
</body>
</html>
posted @ 2020-04-17 15:06  依然范儿特西  阅读(1077)  评论(0编辑  收藏  举报