<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
//定义一个新的new Date()对象
let date = new Date();
window.console.log(typeof date); //对象
window.console.log(date); //获取当前时间点
//获取某一个时间段
let dateBySelf = "2019-01-02";
let dateDeal = new Date(dateBySelf);
window.console.log(dateDeal);
//获取当前年份
let year = date.getFullYear();
window.console.log(year);
/*
*获取当前月份
*当前月份为什么需要+1,因为月份是1-12而对应的常量是0-11,所以需要+1
*/
let month = date.getMonth() + 1;
window.console.log(month);
//获取当前日
let day = date.getDate();
window.console.log(day);
//获取当前小时
let hour = date.getHours();
window.console.log(hour);
//获取当前分钟
let mins = date.getMinutes();
window.console.log(mins);
//获取当前秒钟
let seconds = date.getSeconds();
window.console.log(seconds);
//获取当前毫秒钟
let millSeconds = date.getMilliseconds();
window.console.log(millSeconds);
/*
*当前时间点加上120分钟
*第一步获取当前处于的分钟
*第二步当前的分钟加上120分钟
*第三步用setMinutes()方法
*/
let timeInAll = date.getMinutes() + 120;
let time = date.setMinutes(timeInAll);
window.console.log(date.getHours());
window.console.log(date.getMinutes());
/*
*当前时间点上加上10个小时
*/
let hourInAll = date.getHours() + 10;
let hourChange = date.setHours(hourInAll);
window.console.log(date.getHours());
/*
*当前时间点加上12天
*/
let dayInAll = date.getDate() + 12;
let dayChange = date.setDate(dayInAll);
window.console.log(date.getDate());
/*
*当前的月份增加12个月
*/
let monthInAll = date.getMonth() + 13;
let monthChange = date.setMonth(monthInAll);
window.console.log(date.getMonth());
window.console.log(date.getFullYear());
/*
*当前的年份增加100年
*/
let yearInAll = date.getFullYear() + 100;
let yearChange = date.setFullYear(yearInAll);
window.console.log(date.getFullYear());
//获取当前月份天数
let actualDay = new Date(year, month, 0).getDate();
window.console.log(actualDay);
</script>
</html>
JS new Date( )获取时间和处理时间方法
最新推荐文章于 2024-04-02 17:35:57 发布