/**

*对日期进行格式化,

* @param date 要格式化的日期

* @param format 进行格式化的模式字符串

* 支持的模式字母有:

* y:年,

* M:年中的月份(1-12),

* d:月份中的天(1-31),

* h:小时(0-23),

* m:分(0-59),

* s:秒(0-59),

* S:毫秒(0-999),

* q:季度(1-4)

* @return String

*/

function dateFormat(date, format) {

if(format === undefined){ format = date; date = new Date(); }

var map = {

"M": date.getMonth() + 1, //月份

"d": date.getDate(), //日

"h": date.getHours(), //小时

"m": date.getMinutes(), //分

"s": date.getSeconds(), //秒

"q": Math.floor((date.getMonth() + 3) / 3), //季度

"S": date.getMilliseconds() //毫秒

};

format = format.replace(/([yMdhmsqS])+/g, function(all, t){

var v = map[t];

if(v !== undefined){

if(all.length > 1){

v = '0' + v;

v = v.substr(v.length-2); }

return v;

} else if(t === 'y'){

return (date.getFullYear() + '').substr(4 - all.length);

}

return all;

});

return format;

}

    1. dateFormat('yyyy-MM-dd hh:mm:ss');
    2. dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss')
      来源: http://yaniswang.com/frontend/2013/02/16/dateformat-performance

js格式化日期的更多相关文章

  1. js格式化日期 年月日

    /**                     * 格式化日期                     * @param value                     * @param row  ...

  2. js格式化日期,获取当月的第一天,与最后一天.

    //格式化日期 function setDate(date){   y=date.getFullYear();   m=date.getMonth()+1;   d=date.getDate();   ...

  3. js格式化日期yyyyMMdd

    先来一个土方法: <script>function getyyyyMMdd(){ var d = new Date(); var curr_date = d.getDate(); var ...

  4. js格式化日期工具类

    就是一个工具类  开箱即用 传进一个指定的参数,格式化 //将时间戳格式化 function getMyDate(time){ if(typeof(time)=="undefined&quo ...

  5. JS 格式化日期

    function formatDate(date){ var year=date.getFullYear(); var month=date.getMonth()+1; var date=date.g ...

  6. JS格式化日期时间的方法

    //格式化时间的方法 function format(fmt, date) { var o = { "M+": date.getMonth() + 1, //月份 "d+ ...

  7. js 格式化日期 ("/Date(1400046388387)/")

    var date = new Date(parseInt(str.replace(/\/Date\((-?\d+)\)\//, '$1'))); var d= date.getFullYear() + ...

  8. JS 格式化日期时间

    方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...

  9. js格式化日期时间

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).周(E).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...

随机推荐

  1. Fis3前端工程化之项目实战

    Fis3项目 项目目录结构: E:. │ .gitignore │ fis-conf.js │ index.html │ package.json │ README.md │ ├─material │ ...

  2. 关于Unity3D自定义编辑器的学习

    被人物编辑器折腾了一个月,最终还是交了点成品上去(还要很多优化都还么做).  刚接手这项工作时觉得没概念,没想法,不知道.后来就去看<<Unity5.X从入门到精通>>中有关于 ...

  3. ES5对Array增强的9个API

    为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...

  4. HTML骨架结构

    前面的话   一个完整的HTML文档必须包含3个部分:文档声明.文档头部和文档主体.而正是它们构成了HTML的骨架结构.前面已经分别介绍过文档声明和文档头部,本文将详细介绍构成HTML骨架结构的基础元 ...

  5. spring注解源码分析--how does autowired works?

    1. 背景 注解可以减少代码的开发量,spring提供了丰富的注解功能.我们可能会被问到,spring的注解到底是什么触发的呢?今天以spring最常使用的一个注解autowired来跟踪代码,进行d ...

  6. Jquery(1)

    鼠标点击事件: <input type="button" value="测试" onclick="test()" /> < ...

  7. python 入门笔记

    1.pip包安装 pip install *** pip 中http和https代理设置(/etc/profile) 2.强制保存 :w !sudo tee % 3.cffi是python调用C的包 ...

  8. ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results

    原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...

  9. System.Guid ToString五中格式

    参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx 测试代码: using System; using System.Collectio ...

  10. UVA-146 ID Codes

    It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...