js 時間戳 中國標准時間 年月日 日期之間的轉換


<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>日期格式的轉換</title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
	console.log(new Date()) //獲取中國標准時間      //Wed Feb 13 2019 20:15:44 GMT+0800 (中國標准時間)
	console.log(new Date().getTime()) //標准時間變為時間戳   //1550060144673
	


	//將中國標准時間轉換為年月日格式  ----開始
	function formatTen(num) { 
		return num > 9 ? (num + "") : ("0" + num); 
	} 
	function formatDate(date) { 
		var year = date.getFullYear(); 
		var month = date.getMonth() + 1; 
		var day = date.getDate(); 
		var hour = date.getHours(); 
		var minute = date.getMinutes(); 
		var second = date.getSeconds(); 
		return year + "-" + formatTen(month) + "-" + formatTen(day); 
	}
	//將中國標准時間轉換為年月日格式  ----結束
	
	

        //標准時間變為年月日格式的使用
	var d1=new Date()
	var d2=formatDate(d1)
	console.log(d2)  //2019-02-13



        //在當前日期上加幾天
	function getNextDay(d,t){
	        d = new Date(new Date(d).getTime()+(1000*60*60*24)*t);   //先把標准時間變為時間戳再計算要加的天數
	        //格式化
	        return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();  //轉換一下格式
	 }
	console.log(getNextDay('2019-02-11',6))        //2019-2-17
	</script>
</html>


免責聲明!

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



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