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);

    好,优化完成。

  • 相关阅读:
    ㊣'undefined' 'object' undefined null
    _#【JS】重复的声明
    _#【JSONP】拒绝访问
    _#href与src的区别
    【OOCSS(stubbornella)】
    【JS兼容】一
    ☀【表单】checkbox
    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序”
    python的缩进问题!
    vim粘贴讲的最好的
  • 原文地址:https://www.cnblogs.com/lafitewu/p/8205805.html
Copyright © 2011-2022 走看看