格式化时间日期-在Date上加prototype属性实现

//格式化时间日期的函数  年:YYYY,月:MM,日:DD,他们中间可以用任何字符来链接,例如:var date=new Date().Format('YYYY-MM-DD');下面代码可以作为common的js来在项目中用

(function ($) {

Date.prototype.Format = function (formatStr) {

var str = formatStr;

var Week = ['日', '一', '二', '三', '四', '五', '六'];

str = str.replace(/yyyy|YYYY/, this.getFullYear());

str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));

str = str.replace(/MM/, this.getMonth() + 1 > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1).toString());

str = str.replace(/M/g, this.getMonth() + 1);

str = str.replace(/w|W/g, Week[this.getDay()]);

str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());

str = str.replace(/d|D/g, this.getDate());

str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());

str = str.replace(/h|H/g, this.getHours());

str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());

str = str.replace(/m/g, this.getMinutes());

str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());

str = str.replace(/s|S/g, this.getSeconds());

return str;

}

}(jQuery));

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • [Js获取当前日期时间及其它操作] 日期时间脚本库方法列表 js代码://--------------------...
    JS_swh阅读 3,335评论 0 3
  • 以下是常用的代码收集,学习用。转自豪情博客园 1. PC - js 返回指定范围的随机数(m-n之间)的公式 re...
    自由加咖啡阅读 4,635评论 0 1
  • var regexEnum = { intege : "^-?[1-9]\\d*$", // 整数 intege1...
    a_foo阅读 4,969评论 0 4
  • 飘飘洒洒天阶雨,点点滴滴碧秋木。 叮叮铃铃窗轻响,独倚屋檐鹊咿咿。
    春分时候阅读 1,352评论 0 0
  • 失眠症6号阅读 871评论 0 0