我是靠谱客的博主 傲娇裙子,最近开发中收集的这篇文章主要介绍创建日期对象,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.创建日期对象方法:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 直接new Date(),返回的是当前日期
let date1 = new Date()
console.log(date1);
// 也可以根据指定的日期,创建一个日期对象
let date2 = new Date('1998-8-8')
console.log(date2);
</script>
</body>
</html>

2.也可以根据一个时间戳,创建一个日期对象。时间戳就是:从1970-1-1到指定日期之间的毫秒数:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date(123456789)
console.log(date);
</script>
</body>
</html>

3.getFullYear() 从 Date 对象返回年份:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getFullYear());
</script>
</body>
</html>

4. getMonth() 从 Date 对象返回月份 (0 ~ 11) 注意:0表示1月份,11表示12月份:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getMonth()+1);
</script>
</body>
</html>

5.getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31) 返回的是几号:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getDate());
</script>
</body>
</html>

6.getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)  返回的是周几

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getDay());
</script>
</body>
</html>

7.getHours() 返回小时(0-23):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getHours());
</script>
</body>
</html>

8. getMinutes() 返回分钟(0-59):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getMinutes());
</script>
</body>
</html>

9.getSeconds() 返回秒数(0-59)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getSeconds());
</script>
</body>
</html>

10.getMilliseconds() 返回毫秒(0-999)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getMilliseconds());
</script>
</body>
</html>

11.返回一个当前的时间:代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let date = new Date()
console.log(date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()+" "+date.getMilliseconds());
</script>
</body>
</html>

最后

以上就是傲娇裙子为你收集整理的创建日期对象的全部内容,希望文章能够帮你解决创建日期对象所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部