zoukankan      html  css  js  c++  java
  • js日期时间格式化

     1 formatDate: function (str, fmt) {
     2             if (!str) return '';
     3             fmt = fmt || "yyyy-MM-dd"; //默认只显示年月日
     4             if (this.getIEVersion() > -1) {
     5                 str = str.replace(/-/g, "/")
     6             }
     7             if (typeof str == 'string' && str.indexOf(':') == -1) {
     8                 str += ' 00:00:00';
     9             }
    10             date = new Date(str);
    11             var o = {
    12                     "M+": date.getMonth() + 1, //
    13                     "d+": date.getDate(), //
    14                     "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //
    15                     "H+": date.getHours(), //小时
    16                     "m+": date.getMinutes(), //
    17                     "s+": date.getSeconds(), //
    18                     "q+": Math.floor((date.getMonth() + 3) / 3), //
    19                     "S": date.getMilliseconds() //毫秒
    20                 },
    21                 week = {
    22                     "0": "u65e5",
    23                     "1": "u4e00",
    24                     "2": "u4e8c",
    25                     "3": "u4e09",
    26                     "4": "u56db",
    27                     "5": "u4e94",
    28                     "6": "u516d"
    29                 };
    30             if (/(y+)/.test(fmt)) {
    31                 fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    32             }
    33             if (/(E+)/.test(fmt)) {
    34                 fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "u661fu671f" : "u5468") : "") + week[date.getDay() + ""]);
    35             }
    36             for (var k in o) {
    37                 if (new RegExp("(" + k + ")").test(fmt)) {
    38                     fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    39                 }
    40             }
    41             return fmt;
    42         },

    调用方式:

    formatDate(item.messageDateTime,'yyyy-MM-dd')
  • 相关阅读:
    实参和形参
    location对象
    区别 apply,call
    窗体之间的交互(window.opener)
    我的升级脚本总结
    Create elements
    history 对象
    函数参数的属性:callee
    发布app store流程
    【转】如何生成静态页面的五种方案
  • 原文地址:https://www.cnblogs.com/time1997/p/12788176.html
Copyright © 2011-2022 走看看