// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
 //使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().format("yyyy年MM月dd日"));
alert(new Date().format("MM/dd/yyyy"));
alert(new Date().format("yyyyMMdd"));
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
 

Date.prototype.Format---对Date的扩展的更多相关文章

  1. Date.prototype.format

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

  2. Date.prototype.format,js下的时间格式处理函数

    该方法在date的原型中扩展了format方法,使其可以方便的格式化日期格式输出. Date.prototype.format =function(format) { var o = { , //mo ...

  3. 扩展Date的format方法--格式化日期时间

    Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, "d+& ...

  4. js 的date的format时间,获取当前时间,前一天的日期

    Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...

  5. Date Json格式转换Date格式

    CreateTime=\/Date(1458722493663+0800)\/ var CreateTime="/Date(1458722493663+0800)/";var st ...

  6. 使用Jaxb2进行xml与bean的转义时Date的format设置

    参考http://jackyrong.iteye.com/blog/1826699 JAXB转换JAVA OBJECT到XML的时候,对java.util.Date的转换有些要注意的地方 输出的格式为 ...

  7. js扩展String.prototype.format字符串拼接的功能

    1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...

  8. JavaScriptr -- 常用对象 String, date, prototype

    <script type="text/javascript"> //给已有的对象添加自定义功能 function getMax() { var max = this[0 ...

  9. js中String.prototype.format類似于.net中的string.formitz效果

    String.prototype.format = function(args) { if (arguments.length>0) { var result = this; if (argum ...

随机推荐

  1. 练习九:time.sleep方法

    让python程序暂停预定时间后再运行,需要用到time.sleep方法要求,随便写入一段代码,测试time.sleep方法 import time dict1 = {1:'a',2:'b',3:'c ...

  2. Python时间与日期操作(datetime、time、calendar)

    相关模块 模块 说明 time time是一个仅包含与日期和时间相关的函数和常量的模块,在本模块中定义了C/C++编写的几个类.例如,struct_time类 datetime datetime是一个 ...

  3. HDU 4143 A Simple Problem 分解因式

    求一个最小的正整数x,使得(y + x) (y - x) = n成立 考虑一下n的分解因式. 可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可 但是要枚举到n / 2,这样会超时. ...

  4. HDU 5734 A - Acperience

    http://acm.hdu.edu.cn/showproblem.php?pid=5734 Problem Description Deep neural networks (DNN) have s ...

  5. 行高:line-height图文解析

    行高——line-height 初入前端的时候觉得CSS知道display.position.float就可以在布局上游刃有余了,随着以后工作问题层出不穷,才逐渐了解到CSS并不是几个style属性那 ...

  6. cgkib动态代理详解-不依赖接口,速度快

    1. cglib原理-不依赖接口,速度快 使用ASM字节框架动态生成要代理类的子类,子类重写final以外的方法,织入横切逻辑 2. 示例-实现MethodInterceptor Test.java ...

  7. kotlin查看编译后的Java代码

    java学一下kotlin,由于用的是同样的jvm,那就说明他们的字节码文件应该是一样的,那么,如果我们能看到编译后的文件,那么学的更快了. 操作 1.打开一个.kt文件 2.在Android Stu ...

  8. tomcat7 开机自启动(转)

    转自 http://blog.csdn.net/rainyspring4540/article/details/51861079 环境:win7  tomcat7 开机自启动: 使用管理员打开命令提示 ...

  9. JS常用公共方法封装

    _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...

  10. css动画-小球撞壁反弹

    小球碰到一面壁之后一般都会反弹,反射角=入射角: 其实用css3来实现这个效果也非常简单. 首先,分解一下小球的运动:水平运动和垂直运动. 当小球往右下方向运动时,如果碰到了下面的壁,那么由于碰撞,小 ...