获取日期对象中的指定部分,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>