• js日期控件遇到的问题


    一、问题:

      在web项目里有很多时候需要使用日期控件来完成相关的功能,但是日期控件的日期格式又和我们的需求不符

      那么,就需要我们来自定义日期的格式完成需求

    二、解决:

      1、取月末:

      (1)强制取值:

    //判断每个月最后一天
    function lastDay(cYear,cMouth){
        var year;
        var mouth;
        if (cYear != "" && cMouth!="") {
            year=cYear;
            mouth=cMouth;
        } else {
            var date =new Date();
            year=date.getFullYear();//获取年份
                   mouth=date.getMonth() + 1;
        }
        var days;
        if (mouth==2) {
            //当月份为二月时,根据闰年还是非闰年判断天数
            if (year % 400 == 0 ||  year % 4 == 0 && year % 100 != 0 ) {
                days=29;
            }else{
                days=28;
            };
        }else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
            //月份为:1,3,5,7,8,10,12 时,为大月.则天数为31
            days= 31;
        }else {
            //其他月份,天数为:30.
            days= 30;
        }
        return days;
    };

      (2)使用js日期函数来求:原理涉及到js日期函数的顺序加载问题,先加一,后减一

    function getPreMonthLastDay() {
    
        // debugger;
        var date = new Date();
        var year = date.getFullYear().toString();
        var month = (date.getMonth()).toString();
        var temp = new Date(year,month,0);
        day = (temp.getDate()).toString();
    
        if(month.length == 1) month = "0" + month;
        if(day.length == 1) day = "0" + day;
    
        return year + "-" + month + "-" + day;
        
    }

      2、日期位数不定:(区分年报,月报,日报)

    //初始化日期
    var firstLoad = 0;
    function initReportDateFun(rtypeV){
        $("#reportDate").datebox({
             150,
             required:false,
             onSelect:function(date){
                 //var date = $("#reportDate").datebox("getValue");
                 var selectValue = $('#reportFileType').combobox("getValue");
                 var tvalue = "";
                 if(selectValue == "Y"){
                     tvalue = fmtReportDate(date,"yyyy");
                 }else if(selectValue == "M"){
                     tvalue = fmtReportDate(date,"yyyy-MM");
                 }else{
                     tvalue = fmtReportDate(date,"yyyy-MM-dd");
                 }
                 $("#reportDate").datebox("setText",tvalue);
             },
             parser:function(date){
                 var t = Date.parse(date);
                 if (!isNaN(t)){
                     return new Date(t);
                 } else {
                     return new Date();
                 }
             }
         });
    
        // var selectValue = $('#reportFileType').combobox("getValue");
        if(rtypeV == "Y"){
            $("#reportDate").datebox("setText",fmtReportDate(new Date(),"yyyy"));
        }else if(rtypeV == "M"){
            $("#reportDate").datebox("setText",fmtReportDate(new Date(),"yyyy-MM"));
        }else{
            if(firstLoad>0){
                $("#reportDate").datebox("setText",fmtReportDate(new Date(),"yyyy-MM-dd"));
            }else{
                $("#reportDate").datebox("setText",getPreMonthLastDay());
                firstLoad ++;
            }
        }
        /*if(firstLoad==0){
            clearVal();
            /!*var curr_time = new Date();
            var strDate = curr_time.getFullYear()+"-";
            strDate += curr_time.getMonth()+1+"-";
            strDate += curr_time.getDate();
            $("#reportDate").datebox("setValue", strDate);
            initTxtOrg();*!/
            firstLoad++;
        }else{*/
            initTxtOrg();
        init_report_detail_tab();
        initStatisTable();
        //}
    }

      出现的问题:

        之前没有加parser】函数,会出现一个问题,选择5月份的日期后,日期框还是保留【今天】,日期和框显示的数据不一致

        可能是由于处理时间的时候会把数据作文文本显示,但是控件并不能解析这个字符串而让面板和显示的字符串日期保持一致

        使用【parser】函数后,每次加载完成,会把字符串解析成一个日期函数以供控件加载初始化数据

  • 相关阅读:
    centos 网络开启
    ubuntu server 服务器部署(二) mysql 安装配置手记
    ubuntu server 服务器部署(一) jdk 安装配置手记
    网络管理
    磁盘配额
    创建raid0
    创建raid5
    逻辑卷快照
    LVM(逻辑卷)
    1.计算机基础
  • 原文地址:https://www.cnblogs.com/hackxiyu/p/9082085.html
Copyright © 2020-2023  润新知