|
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="keywords" content="js,date,format">
<meta name="description" content="js date format">
<title>js date format</title>
<style type="text/css">
*{margin:0;padding:0;}
#wrapper{margin:50px auto;width:300px;border:1px solid green;}
#wrapper div + div{margin:2px 0 0 2px;border-top:1px solid gray;}
#wrapper div:nth-child(even){color:#666;}
</style>
</head>
<body>
<%
Date birthday = new Date();
request.setAttribute("birthday", birthday);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String birthStr = sdf.format(birthday);
request.setAttribute("birthStr", birthStr);
out.println(birthday);
%>
<div id="wrapper">
<div id="dateformat"></div>
<div id="dateformat2"></div>
<div id="dateformat3"></div>
<div id="dateformat4"></div>
<div id="dateformat5"></div>
<div id="dateformat6"><c:out value="${birthday}"/></div>
<div id="dateformat7"><fmt:formatDate value="${birthday}" pattern="yyyy-MM-dd"/></div>
<div id="dateformat8"></div>
<div id="dateformat9"><c:out value="${birthStr}"/></div>
</div>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), //日
"H+": this.getHours(), //24小时制
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //12小时制
"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 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");
var time4 = new Date().format("yyyy-MM-dd HH:mm:ss S");
var time5 = new Date(1145667888).format("yyyy-MM-dd HH:mm:ss S");
$("#dateformat").text(time1);
$("#dateformat2").text(time2);
$("#dateformat3").text(time3);
$("#dateformat4").text(time4);
$("#dateformat5").text(time5);
$("#dateformat8").text(new Date("${birthday}").format("yyyy-MM-dd HH:mm:ss S"));
</script>
</body>
</html>
|