js的Date对象

Date 对象用于处理日期与时间。可以通过 new 关键词来定义 Date 对象。

有四种方式初始化日期:

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

实例化例子:

var today = new Date()
var d1 = new Date("October 13, 1975 11:13:00")
var d2 = new Date(79,5,24)
var d3 = new Date(79,5,24,11,33,0)

Date的常用get方法

方法描述
getFullYear()从 Date 对象以四位数字返回年份。
getMonth()从 Date 对象返回月份 (0 ~ 11)。要加1才会得到真正的月份。
getDate()从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay()从 Date 对象返回一周中的某一天 (0 ~ 6)。
getHours()返回 Date 对象的小时 (0 ~ 23)。
getMinutes()返回 Date 对象的分钟 (0 ~ 59)。
getSeconds()返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds()返回 Date 对象的毫秒(0 ~ 999)。
getTime()返回 1970 年 1 月 1 日至今的毫秒数。

html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>js的Date()对象</title>
</head>
<body>


<script src="jquery.js"></script>		

<script type="text/javascript">

	console.log('Date 对象:',new Date()) // Date 对象用于处理日期与时间。
	console.log('年:', new Date().getFullYear()) // 可返回一个表示年份的 4 位数字。
	console.log('月:', new Date().getMonth()+1) // getmonth()的返回值是 0(一月) 到 11(十二月) 之间的一个整数!获取的其实是索引值,他的值是从0开始的,所以要加1才会得到真正的月份。
	console.log('日:', new Date().getDate()) // 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
	console.log('星期:', new Date().getDay()) // 从 Date 对象返回一周中的某一天 (0 ~ 6)。
	console.log('小时:', new Date().getHours()) // 返回 Date 对象的小时 (0 ~ 23)。
	console.log('分钟:', new Date().getMinutes()) // 返回 Date 对象的分钟 (0 ~ 59)。
	console.log('秒:', new Date().getSeconds()) // 返回 Date 对象的秒数 (0 ~ 59)。
	console.log('毫秒:', new Date().getMilliseconds()) // 返回 Date 对象的毫秒(0 ~ 999)。
	console.log('1970 年 1 月 1 日至今的毫秒数:', new Date().getTime()) // 返回 1970 年 1 月 1 日至今的毫秒数。
</script>
</body>
</html>

输出如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值