博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript 时间格式
阅读量:7053 次
发布时间:2019-06-28

本文共 2292 字,大约阅读时间需要 7 分钟。

方法1:

Date.prototype.Format = function (fmt) {      var o = {        "M+": this.getMonth() + 1, //月份         "d+": this.getDate(), //日         "h+": this.getHours()%12==0?12:this.getHours()%12, //小时         "H+": this.getHours(),        "m+": this.getMinutes(), //分         "s+": this.getSeconds(), //秒         "q+": Math.floor((this.getMonth() + 3) / 3), //季度         "f": 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 time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");  var time3 = new Date().Format("yyyy-MM-dd hh:mm:ss:f"); var time4=new Date().toLocaleString(); console.log(time1);console.log(time2);console.log(time3);console.log(time4);

方法2:

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));       //    this.setMonth(this.getMonth()+1);    str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());       str=str.replace(/M/g,this.getMonth());         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());         str=str.replace(/f/,this.getMilliseconds());      return str;   }

跟上面的一样调用

但是小时没有12小时制的了

 

http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

 

你可能感兴趣的文章
Mozilla将从3月31日起实行插件“点击运行”机制
查看>>
《可穿戴创意设计:技术与时尚的融合》一一1.3 可穿戴设备和艺术
查看>>
JavaScript 实现的人脸检测方法
查看>>
Qubes OS 创始人认为英特尔 x86 是有害的
查看>>
《ANSYS Workbench有限元分析实例详解(静力学)》——2.3 工程流程图
查看>>
《现代体系结构上的UNIX系统:内核程序员的对称多处理和缓存技术(修订版)》——1.7 习题...
查看>>
《音乐达人秀:Adobe Audition CC实战222例》——1.2 从双卡录音机到多轨录音软件...
查看>>
年度回顾 看看 2016 年编程语言发展趋势
查看>>
《电路分析导论(原书第12版)》一2.7 电池寿命因素
查看>>
Java异常处理的误区和经验总结
查看>>
在 LinkedIn 之后,微软本周又收购了一家公司
查看>>
C++11新特性中的匿名函数Lambda表达式的汇编实现分析(一)
查看>>
Ubuntu on Windows 10 工作方式全解析
查看>>
《技术的潜能:商业颠覆、创新与执行》一一2.6前路曲折
查看>>
《OSPF和IS-IS详解》一6.3 复习题
查看>>
《脱颖而出——成功网店经营之道》一1.4 网店的特点
查看>>
开发者声称利用 FreeBSD 内核漏洞越狱 PS4
查看>>
API经济打通企业内外互通的任督二脉?
查看>>
Ubuntu 软件中心因推广非自由软件被批评
查看>>
《jQuery UI 开发指南》——第2章 选项卡2.1 选项卡的基本用法
查看>>