javascript中和日期相关的对象(Date)

Date在创建的时候必须要创建对象。创建对象要用到关键字:new

方法:

------------------------------------------获得时间

getDate()  从Date对象返回一个月中的某一天(1-31)  

getDay()  从Date对象返回一周中的某一天(0-6)

getMonth() 从Date对象返回月份(0-11)

getFullYear() 从Date对象以四位数字返回年份

getYear()  从Date对象以两位或四位数字返回年份

getHours() 返回Date对象的小时(0-23)

getMinutes() 返回对象的分钟(0-59)

getSeconds() 返回对象的秒数(0-59)

getTime() 返回现在到1970的毫秒值

toLocaleString() 使日期进行格式化

----------------------------------设置时间

setDate()  设置Date对象一个月中的某一天(1-31)  

setMonth() 设置Date对象月份(0-11)

setFullYear() 设置Date对象以四位数字年份

setYear()  设置Date对象以两位或四位数字年份

setHours() 设置Date对象的小时(0-23)

setMinutes() 设置对象的分钟(0-59)

setSeconds() 设置对象的秒数(0-59)

-----------------------其他方法

toString() 把Date对象转换为字符串

案例:


<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>js当中搞的日期对象(Date)</title>
 </head>
 <body>
	<script type="text/javascript">
		//创建一个Date对象
		var d=new Date();
		document.write(d+"<br/>");//放回当前日期和时间
		document.write(d.getDate()+"<br/>");//返回一个月中的日期
		document.write(d.getDay()+"<br/>");//返回一个星期中的星期几
		document.write(d.getFullYear()+"<br/>");//返回四个数字的年
		document.write(d.getYear()+"<br/>");//返回两位数字的年,这里应该是用现在的年份减去1990
		document.write(d.getMonth()+"<br/>");//返回一年中的月份
		document.write(d.getHours()+"<br/>")//返回现在的小时
		document.write(d.getMinutes()+"<br/>")//返回现在的分钟数
		document.write(d.getSeconds()+"<br/>");//返回现在的秒钟数
		document.write(d.getTime()+"<br/>");//返回现在到1970的毫秒值
		document.write(d.toLocaleString()+"<br/>");//使日期格式化
		</script>
 </body>
</html>
Sat Apr 07 2018 20:06:38 GMT+0800 (中国标准时间)
7
6
2018
118
3
20
6
38
1523102798004
‎2018‎年‎4‎月‎7‎日‎ ‎20‎:‎06‎:‎38



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值