JavaScript 格式化时间日期

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Format Date </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <mce:script type="text/javascript"><!-- /** * 时间对象的格式化; */ Date.prototype.format = function(format) { /* * eg:format="yyyy-MM-dd hh:mm:ss"; */ var o = { "M+" :this.getMonth() + 1, // month "d+" :this.getDate(), // day "h+" :this.getHours(), // hour "m+" :this.getMinutes(), // minute "s+" :this.getSeconds(), // second "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter "S" :this.getMilliseconds() // millisecond } if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length)); } for ( var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; } function testDate(){//测试 var testDate = new Date(); alert(testDate.format("yyyy-MM-dd hh:mm:ss"));//yyyy 一定得是小写 alert(testDate.format("yyyy年MM月dd日hh小时mm分ss秒")); alert(testDate.format("yyyy年MM月dd日")); alert(testDate.format("MM/dd/yyyy")); alert(testDate.format("yyyyMMdd")); alert(testDate.format("hh:mm:ss")); } // --></mce:script> </HEAD> <BODY> <input type="button" onclick="testDate();" value="testDate"/><!-- 测试按钮 --> </BODY> </HTML>

 

原文来自:    http://lovely.javaeye.com/blog/507798  本人经过实践 , 发了个比较全的代码上来

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

HIVE 时间操作函数

日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec