zoukankan      html  css  js  c++  java
  • timeDate.js 插件优化

    昨天写的时候,在下面代码处就碰到了问题,

    如下:

    if(data.zero) {
      if(data.Month_zero) {
         if(time.month < 10) {
             time.month = "0" + time.month;
           }
        }
        if(data.Day_zero) {
           if(time.day < 10) {
               time.day = "0" + time.day;
           }
        }
        if(data.Hours_zero) {
           if(time.hours < 10) {
               time.hours = "0" + time.hours;
           }
        }
        if(data.Min_zero) {
           if(time.minutes < 10) {
               time.minutes = "0" + time.minutes;
           }
        }
        if(data.Second_zero) {
           if(time.seconds < 10) {
               time.seconds = "0" + time.seconds;
            }
        }
    }

    因为代码耦合程度太高,所以想用一个函数来封装,如下:

    function Zero(a) {
        if(a < 10) {
            a = "0" + a;
        }
    }    

    却无法让其改变原始的参数值,挣扎了许久,就放下了。

    今天经过一天的征战,终于得以解决。源码如下:

    timeDate.js

    /*
     * timeDate.js
     * by LafiteWu    
     * by QQ:863512936
     * 2018-01-05 v1.1
     */
    
    
    (function($) {
        $.fn.timeDate = function(options) {
            var that = $(this);
            var defaults = {
                show: "Y-M-D-W", //显示形式
                 zero: true, // 是否开启零,也是总开关,当它值为false时,下面的值无效
                 Month_zero: true, // 月份是否加零
                 Day_zero: true, // 日期上是否加零
                 Hours_zero: true, // 小时前是否加零
                 Min_zero: true, // 分钟前是否加零
                 Second_zero: true // 秒钟前加零
             };
             var data = $.extend(defaults,options);
             var now = new Date(); 
             var weekday = ["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
             var time = {
                 year: now.getFullYear(),
                 month: now.getMonth()+1,
                 day: now.getDate(),
                 week: weekday[now.getDay()],
                 hours: now.getHours(),
                 minutes: now.getMinutes(),
                 seconds: now.getSeconds(),
                   Zero:function(a) {
                        if(a < 10) {
                           return  a = "0" + a;
                        }else {
                  return a;
                } }, Html:
    function(a) { that.html(a); } }; if(data.zero) { if(data.Month_zero) { time.month = time.Zero(time.month); } if(data.Day_zero) { time.day = time.Zero(time.day); } if(data.Hours_zero) { time.hours = time.Zero(time.hours); } if(data.Min_zero) { time.minutes = time.Zero(time.minutes); } if(data.Second_zero) { time.seconds = time.Zero(time.seconds); } } switch(data.show) { case("Y"): time.Html(time.year); break; case("M"): time.Html(time.month); break; case("D"): time.Html(time.day); break; case("H"): time.Html(time.hours); break; case("m"): time.Html(time.minutes); break; case("S"): time.Html(time.seconds); break; case("W"): time.Html(time.week); break; case("Y-M"): time.Html(time.year+"年"+time.month+"月"); break; case("Y/M"): time.Html(time.year+"/"+time.month); break; case("M-D"): time.Html(time.month+"月"+time.day+"日"); break; case("M/D"): time.Html(time.month+"/"+time.day); break; case("M-D-W"): time.Html(time.month+"月"+time.day+"日 "+time.week); break; case("M/D/W"): time.Html(time.month+"/"+time.day+" "+time.week); break; case("Y-M-D"): time.Html(time.year+"年"+time.month+"月"+time.day+"日"); break; case("Y-M-D-W"): time.Html(time.year+"年"+time.month+"月"+time.day+"日 "+time.week); break; case("Y/M/D/W"): time.Html(time.year+"/"+time.month+"/"+time.day+" "+time.week); break; case("Y/M/D"): time.Html(time.year+"/"+time.month+"/"+time.day); break; case("H-m"): time.Html(time.hours+":"+time.minutes); break; case("H-m-S"): time.Html(time.hours+":"+time.minutes+":"+time.seconds); break; default: throw new Error("Check if your format is correct"); } } })(jQuery);

    好,优化完成。

  • 相关阅读:
    【解决火车轮播图小圆点跳的问题】传统轮播图-三位法
    jq龙禧轮播图
    QT MSVC环境中添加QWT
    XDMA ip core的使用
    PCIe基础知识与例程分析----PIO_demo
    Day04 (四)_TCP文件传输设计
    Day04 (三)_UDP传输端设计
    Day04 (二)_TCP传输客户器端设计
    Day04 (一)_TCP传输服务器端设计
    Day03 (下)_Qt文件系统
  • 原文地址:https://www.cnblogs.com/lafitewu/p/8205805.html
Copyright © 2011-2022 走看看