JS 、JavaScript获取当前日期时间及获取当前月第一天和最后一天
JS 、JavaScript获取当前日期时间及获取当前月第一天和最后一天
·
文章目录
JavaScript获取当前时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div></div>
</body>
</html>
<script type="text/javascript">
/**
* 获取当前时间
*/
function getCurrentDate() {
let currentDate = new Date();
let fullYear = currentDate.getFullYear();// 获取当前年份(4位)
console.log(fullYear);
let month = currentDate.getMonth();// 获取当前月份(0-11,0代表1月)
console.log(month);
let date = currentDate.getDate();// 获取当前日(1-31)
console.log(date);
let day = currentDate.getDay();// 获取当前星期X(0-6,0 代表星期天)
console.log(day);
let time = currentDate.getTime();// 获取当前时间(从1970.1.1开始的毫秒数)
console.log(time);
let hours = currentDate.getHours();// 获取当前小时数(0-23)
console.log(hours);
let minutes = currentDate.getMinutes();// 获取当前分钟数(0-59)
console.log(minutes);
let seconds = currentDate.getSeconds();// 获取当前秒数(0-59)
console.log(seconds);
let milliseconds = currentDate.getMilliseconds();// 获取当前毫秒数(0-999)
console.log(milliseconds);
let localeDateString = currentDate.toLocaleDateString();// 获取当前日期
console.log(localeDateString);
let localeTimeString = currentDate.toLocaleTimeString();// 获取当前时间
console.log(localeTimeString);
let localeString = currentDate.toLocaleString();// 获取当前日期与时间
console.log(localeString);
}
// 调用函数
getCurrentDate();
</script>
JavaScript获取当前时间的一年前时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div></div>
</body>
</html>
<script type="text/javascript">
/**
* 获取当前时间的一年前时间
*/
function getYearAgo() {
const currentDate = new Date();
currentDate.setFullYear(currentDate.getFullYear() - 1)
let localeString = currentDate.toLocaleString();
console.log("当前时间的一年前时间:" + localeString);
}
// 调用函数
getYearAgo();
</script>
JavaScript获取当前时间的半年前时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div></div>
</body>
</html>
<script type="text/javascript">
/**
* 获取当前时间的半年前时间
*/
function getYearHalf() {
const currentDate = new Date();
currentDate.setMonth(currentDate.getMonth() - 6);
// currentDate.setDate(currentDate.getDate() -1)
let localeString = currentDate.toLocaleString();
console.log("当前时间的半年前时间:" + localeString);
}
// 调用函数
getYearHalf();
</script>
JavaScript获取当前时间至一年前的所有年月
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div></div>
</body>
</html>
<script type="text/javascript">
/**
* 获取当前时间至一年前所有的年月
* @returns {*[]}
* 如输出 ["2022-07", "2022-08", "2022-09", "2022-10", "2022-11", "2022-12", "2023-01", "2023-02", "2023-03", "2023-04", "2023-05", "2023-06", "2023-07"]
*/
function getLastMonth() {
let result = [];
for (let i = 0; i < 13; i++) {
const currentDate = new Date();
currentDate.setDate(1);// 将当前时间的日期设置成第一天
let month = currentDate.getMonth() - i;
currentDate.setMonth(month)
let returnMonth = currentDate.getMonth() + 1;
returnMonth = returnMonth < 10 ? "0" + returnMonth : returnMonth;//自定义输出日期的格式
// result.push(currentDate.getFullYear() + "年" + returnMonth+"月");
result.push(currentDate.getFullYear() + "-" + returnMonth);
}
return result;
}
let info = getLastMonth().reverse(); //反转
console.log(info)
</script>
JavaScript获取当前月第一天和最后一天
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div></div>
</body>
</html>
<script type="text/javascript">
// 获取当前月第一天
function getFirstDay() {
const date = new Date();
// 将当前时间的日期设置成第一天
date.setDate(1);
let y = date.getFullYear();
let m = date.getMonth() + 1;
// 月份补 0
m = m < 10 ? '0' + m : m;
let d = date.getDate();
// 日期补 0
d = d < 10 ? '0' + d : d;
let firstDay = [y, m, d].join('-');
return firstDay
}
console.log("获取当前月第一天:" + getFirstDay())
// 获取当前月最后一天
function getLastDay() {
const date = new Date();
let y = date.getFullYear();
let m = date.getMonth() + 1;
// 月份补 0
m = m < 10 ? '0' + m : m;
// new Date(y, m, 0) 获取当前月的最后一天,参数 0 代表上个月的最后一天
let d = new Date(y, m, 0).getDate();
// 日期补 0
d = d < 10 ? '0' + d : d;
let lastDay = [y, m, d].join('-');
return lastDay
}
console.log("获取当前月最后一天:" + getLastDay())
// 获取当前月第一天和最后一天
function getFirstLastDay1(date) {
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? '0' + m : m;
// 设置为日期1号
date.setDate(1);
let firstDay = date.getDate();
firstDay = firstDay < 10 ? '0' + firstDay : firstDay;
let first = [y, m, firstDay].join('-');
// 获取当前月的最后一天,参数 0 代表上个月的最后一天
let lastDay = new Date(y, m, 0).getDate();
let last = [y, m, lastDay].join('-');
return first + " 至 " + last
}
console.log("起止日期:" + getFirstLastDay1(new Date()))
// 获取当前月第一天和最后一天
function getFirstLastDay2(dateString) {
let year = Number(dateString.slice(0, 4));
let month = Number(dateString.slice(4, 6));
let date = new Date(year, month - 1);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? '0' + m : m;
// 设置为日期1号
date.setDate(1);
let firstDay = date.getDate();
firstDay = firstDay < 10 ? '0' + firstDay : firstDay;
let first = [y, m, firstDay].join('-');
// 获取当前月的最后一天,参数 0 代表上个月的最后一天
let lastDay = new Date(year, month, 0).getDate();
let last = [y, m, lastDay].join('-');
return first + " 至 " + last
}
console.log("起止日期:" + getFirstLastDay2("202309"))
</script>
更多推荐
已为社区贡献1条内容
所有评论(0)