zoukankan      html  css  js  c++  java
  • Angularjs中比较实用的DateFormat库

    angular.module('newApp')
      .factory('dateUtil', function() {
        var symbolMap = {
          'MM': function(date) {
            if (typeof(date) === 'string') {
              var d = new Date(date);
              return d.getMonth();
            }
            return date.getMonth() + 1;
          },
          'mm': function(date) {
            if (typeof(date) === 'string') {
              var d = new Date(date);
              return d.getMinutes();
            }
            return date.getMinutes();
          },
          'YY': function(date) {
            if (typeof(date) === 'string') {
              var d = new Date(date);
              return d.getFullYear();
            }
            return date.getFullYear();
          },
          'ss': function(date) {
            if (typeof(date) === 'string') {
              var d = new Date(date);
              return d.getSeconds();
            }
            return date.getSeconds();
          },
          'hh': function(date) {
            if (typeof(date) === 'string') {
              var d = new Date(date);
              return d.getHours();
            }
            return date.getHours();
          },
          'dd': function(date) {
            if (typeof(date) === 'string') {
              var d = new Date(date);
              return d.getDate();
            }
            return date.getDate();
          }
        };
    
        function _makeNchar(char, n) {
          var str = [];
          while (n--) {
            str.push(char);
          }
          return str.join('');
        }
    
        function alignNumber(num, len, char) {
          num = num + '';
          if (num.length > len) {
            return num;
          } else {
            return _makeNchar(char, len - num.length) + num;
          }
        }
    
        function getRelativeDate(offset, date) {
          var relativeDate = new Date(date),
            dateValue = relativeDate.getDate() + offset;
          relativeDate.setDate(dateValue);
          return relativeDate;
        }return {
          format: function(date, fmtStr) {
            if (fmtStr) {
              return fmtStr.replace((/(MM|mm|YY|ss|hh|dd)/g), function(s) {
                return alignNumber(symbolMap[s](date), 2, '0');
              });
            }
          },
          getRelativeDate: getRelativeDate
        };
      });
  • 相关阅读:
    取消chrome(谷歌浏览器)浏览器下最小字体限制
    函数声明方式及作为值的函数
    常用的正则表达式
    8腾讯云服务器账号密码
    errno -4058 and npm WARN enoent ENOENT 解决方案
    node gyp的问题
    npm 版本问题
    无缘无故出现npm 解析异常的的问题 解决方案
    idea注册码
    以太坊测试网络 账号密码
  • 原文地址:https://www.cnblogs.com/ccblogs/p/5266489.html
Copyright © 2011-2022 走看看