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')
  • 相关阅读:
    7.13dfs例题:部分和
    7.12dfs例题:数独游戏
    1.2题解:如何找数组中唯一成对的那个数(位运算)
    左程云Java算法(1)
    SQL基本语句增删改查
    Python spyder Ipython console 连接失败问题
    VBA——Msgbox
    python 字符串
    Scrapy-selectors总结
    文字单行居中,多行居左/居右
  • 原文地址:https://www.cnblogs.com/time1997/p/12788176.html
Copyright © 2011-2022 走看看