zoukankan      html  css  js  c++  java
  • JS

    // date必填, pattern默认'yyyy-MM-dd HH:mm:ss'
    function dateFormat (date, pattern) {
    var week = {'0':'日', '1':'一', '2':'二', '3':'三', '4':'四', '5':'五', '6':'六'};
    pattern = pattern == null ? 'yyyy-MM-dd HH:mm:ss' : pattern;
    var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(pattern)) {
    pattern = pattern.replace(RegExp.$1, (date.getFullYear() + '').substring(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(pattern)) {
    pattern = pattern.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[date.getDay() + '']);
    }
    for (var k in o) {
    if (new RegExp('(' + k + ')').test(pattern)) {
    pattern = pattern.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substring(('' + o[k]).length)));
    }
    }
    return pattern;
    }

    调用Demo:

    dateFormat(new Date());
    // 输出: "2017-07-12 17:49:44"

    dateFormat(new Date(), 'yyyy年 MM月 dd日 HH时 mm分 ss秒 S毫秒 周E 第q季度');
    // 输出: "2017年 07月 12日 17时 55分 49秒 360毫秒 周三 第3季度"


  • 相关阅读:
    Emacs 安装 jedi
    PHP+ MongoDB
    Debian 7 安装 Emacs 24.3
    在Code first中使用数据库里的视图
    Emacs安装auto-complete
    Debian 7.4 中配置PHP环境
    ASP.NET MVC 下载列表
    JDicom使用指南
    Windows常用的DOS命令
    Entity Framework问题总结
  • 原文地址:https://www.cnblogs.com/andremao/p/7156713.html
Copyright © 2011-2022 走看看