JS----日期對象

獲取日期對象中的指定部分,js1.6開始加入toLocaleFormat的方法,但是IE只支持JS1.3,所以不支持此方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
 <script type="text/javascript" language="javascript">
     var now=new Date();      //得到系統當前時間
	 var strDate=now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate();
	 var strTime=now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+":"+now.getMilliseconds();
	 document.write("現在的時間是:"+strDate+"   "+strTime+"<br>");
	 document.write("今天是星期"+now.getDay()+"<br>");
  </script>
</body>
</html>


============================================================

設置日期對象中的指定部分:

注意toLocaleDateString()方法

與:toLocaleString()方法的不同,2者雖然都是顯示本地時,但是顯示格式不同

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
 <script type="text/javascript" language="javascript">
     var now=new Date();      //得到系統當前時間
	 document.write("當前時間爲:"+now.toLocaleString()+"<br>"); 
	 
	 
	 now.setFullYear(now.getFullYear()-1);
	 document.write("1年前的這個時候:"+now.toLocaleString()+"<br>");
	 
	 
	 now.setMonth(now.getMonth()+1);
	 document.write("1個月後:"+now.toLocaleDateString()+"<br>");
	
  </script>
</body>
</html>


===================================================

對象中帶有參數:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body>
 <script type="text/javascript" language="javascript">
     var date1=new Date(2009,3,19); 
	 var date2=new Date(2009,3,19,17,48,2); 
	 document.write("date1爲:"+date1.toLocaleString()+"<br>"); 
	 document.write("date2爲:"+date2.toLocaleString()+"<br>");
	 document.write("date2另外一種顯示爲:"+date2.toLocaleDateString()+"<br>");
  </script>
</body>
</html>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章