js獲取當前時間並格式化


js獲取當前時間並格式化

	<!DOCTYPE html>
	<html>

	<head>
	<meta charset="UTF-8">
	<title>js獲取當前時間並格式化</title>
	<script type="text/javascript" src="../js/jquery-1.10.2.min.js"></script>
	<script>
		/**************************************時間格式化處理************************************/
		function dateFtt(fmt, date) { //author: meizz   
			var o = {
				"M+": date.getMonth() + 1, //月份   
				"d+": date.getDate(), //日   
				"h+": date.getHours(), //小時   
				"m+": date.getMinutes(), //分   
				"s+": date.getSeconds(), //秒   
				"q+": Math.floor((date.getMonth() + 3) / 3), //季度   
				"S": date.getMilliseconds() //毫秒   
			};
			if(/(y+)/.test(fmt))
				fmt = fmt.replace(RegExp.$1, (date.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;
		}
		function time() {
			var myDate = new Date(); //獲取系統當前時間
			alert(myDate);
			var a = $("#nowtime").val();
			console.log(a);
			alert(a);
			//dateFtt("yyyy-MM-dd hh:mm:ss",myDate);//直接調用公共JS里面的時間類處理的辦法     
			alert(dateFtt("yyyy-MM-dd hh:mm:ss", myDate));
			$("#nowtime").val(dateFtt("yyyy-MM-dd hh:mm:ss", myDate));
		}
		//time();
		//
		</script>
	</head>

	<body>
		<button onclick="time()">點擊</button> <br/> 時間測試:
		<input type="text" id="nowtime" value="111" />
	</body>
	</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM