JavaScript-2.内置对象---简单脚本之弹出对话框显示当前时间 ---ShinePans

<html>
	<head>
	<meta http-equiv="content-type" content="text/html; charset=GB2312" />
	<title> 2-1 Tell User The Time </title>
	<!--脚本部分-->
	<script type="text/javascript">
		date_object=new Date();
		what_to_say=date_object.toLocaleString();
		alert(what_to_say);
		</script>
	</head>
	<body style="overflow:auto;">
	</body>
</html>



获取当前日期时间:

<html>
	<head>
		<meta htp-equiv="content-type" content="text/html;charset=GB2312">
		<title>
			2-2 获取当前日期时间
		</title>
		<script type="text/javascript">
			dt=new Date();
			what_to_say="今天是"+dt.getYear()+"年"+(dt.getMonth()+1)+"月"+dt.getDate()+"日"+",如今时间是"+dt.getHours()+"点"+dt.getMinutes()+"分"+dt.getSeconds()+"秒";
			alert(what_to_say);
			</script>
	</head>
	<body style="overflow:auto;">
	</body>
</html>


\

临时还不知道为什么显示的是 114年而不是2014年,望高手能解释下!


内置对象:

JavaScript有非常多预先定义好的对象,能够直接在脚本中使用,这样的对象被称为内置对象, 如Date 就是一个内置对象

内置对象 Date 的方法:

getDate 返回日期值(1-31)
getDay 返回星期值(0-6)
getFullYear 返回4位数年份值
getHours 返回小时值
getMilliseconds 返回毫秒值
getMinutes 返回分钟值
getMonth 返回月份
getSeconds 返回秒
getTime 返回自1970年1月1日至今的毫秒数
getTimezoneOffset 返回当前时区
getUTCDate 返回UTC(世界标准时间)日期值
getUTCDay 返回UTC星期值
GetUTCFullYear 返回UTC 4位年份值
getUTCHours 返回UTC小时值
get]UTCMilliseconds 返回UTC毫秒时
getUTCMinutes 返回UTC分钟值
getUTCMonth 返回UTC 月份
getUTCSeconds 返回UTC秒
getVarDate 返回支持COM组件的日期值
getYear 返回年份
parse 返回自1970年1月1日到解析日期的毫秒数
setMinutes 设置分
setMonth 设置月份
setMilliseconds 设置毫秒数
setDate 设置日期
setFullYear 设置年份
setHours 设置小时数
setSeconds 设置秒
setTime  设置时间
setUTCDate 设置UTC日期
setUTCFullYear 设置UTC年份
setUTCHours 设置UTC小时
setUTCMilliseconds 设置UTC毫秒数
setUTCMonth 设置UTC月份
setUTCMinutes 设置UTC分钟数
setUTCSeconds 设置秒
setYear 设置年份
toDateString 返回日期的字符串描写叙述
toGMTString 返回日期时间的UTC字符串描写叙述
toLocaleDateString 返回日期的本地系统字符串描写叙述
toLocaleString 返回日期时间的本地系统字符串描写叙述
toLocaleTimeString 返回时间的本地系统描写叙述
toString 返回日期时间的字符串,描写叙述
toTimeString 返回日期时间的字符串描写叙述
toUTCString 返回日期时间的UTC字符串描写叙述,等同于toGMTString
UTC 返回自1970年1月1日至解析的日期的毫秒数
valueOf 返回自1970年1月1日至解析的日期毫秒数
   

在获得 Date对象后,使用"." 操作符引用其全部的属性或方法

如:

获取当前年份 的做法:

dateobj =new Date();

theYear =dateObj.getYear();

原文地址:https://www.cnblogs.com/mqxnongmin/p/10540872.html