JavaScript获取当前日期
在JavaScript中,我们可以使用内置的Date对象来获取当前日期和时间。Date对象表示日期和时间,可以获取当前日期、时间、年、月、日、小时、分钟、秒等信息。在本文中,我们将详细介绍如何使用JavaScript获取当前日期。
获取当前日期
1. 获取当前日期的年、月、日
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
console.log(`当前日期:{year}年{month}月${day}日`);
Output:
2. 获取当前日期的星期几
const now = new Date();
const weekDay = now.getDay();
const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
console.log(`今天是${weekDays[weekDay]}`);
Output:
3. 获取当前日期的时间
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
console.log(`当前时间:{hours}时{minutes}分${seconds}秒`);
Output:
格式化日期
4. 格式化日期为YYYY-MM-DD
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const formattedDate = `{year}-{month}-{day}`;
console.log(`格式化后的日期:{formattedDate}`);
Output:
5. 格式化日期为YYYY年MM月DD日
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const formattedDate = `{year}年{month}月{day}日`;
console.log(`格式化后的日期:{formattedDate}`);
Output:
6. 格式化日期为YYYY/MM/DD HH:mm:ss
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const formattedDate = `{year}/{month}/{day}{hours}:{minutes}:{seconds}`;
console.log(`格式化后的日期:${formattedDate}`);
Output:
比较日期
7. 比较两个日期是否相等
const date1 = new Date('2022-01-01');
const date2 = new Date('2022-01-01');
if (date1.getTime() === date2.getTime()) {
console.log('两个日期相等');
} else {
console.log('两个日期不相等');
}
Output:
8. 比较两个日期的大小
const date1 = new Date('2022-01-01');
const date2 = new Date('2022-01-02');
if (date1.getTime() < date2.getTime()) {
console.log('date1 < date2');
} else if (date1.getTime() > date2.getTime()) {
console.log('date1 > date2');
} else {
console.log('date1 = date2');
}
Output:
计算日期
9. 计算两个日期相差的天数
const date1 = new Date('2022-01-01');
const date2 = new Date('2022-01-10');
const diffTime = Math.abs(date2.getTime() - date1.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(`两个日期相差${diffDays}天`);
Output:
10. 计算指定日期加上指定天数后的日期
const date = new Date('2022-01-01');
const daysToAdd = 10;
date.setDate(date.getDate() + daysToAdd);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const newDate = `{year}-{month}-{day}`;
console.log(`加上{daysToAdd}天后的日期:${newDate}`);
Output:
11. 计算指定日期加上指定月数后的日期
const date = new Date('2022-01-01');
const monthsToAdd = 2;
date.setMonth(date.getMonth() + monthsToAdd);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const newDate = `{year}-{month}-{day}`;
console.log(`加上{monthsToAdd}个月后的日期:${newDate}`);
Output:
其他操作
12. 获取当前时间戳
const timestamp = new Date().getTime();
console.log(`当前时间戳:${timestamp}`);
13. 将时间戳转换为日期
const timestamp = 1641019815000;
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
const formattedDate = `{year}-{month}-{day}{hours}:{minutes}:{seconds}`;
console.log(`时间戳转换后的日期:${formattedDate}`);
Output:
14. 获取当前日期的下一个月的第一天
const now = new Date();
const nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
const year = nextMonth.getFullYear();
const month = String(nextMonth.getMonth() + 1).padStart(2, '0');
const day = String(nextMonth.getDate()).padStart(2, '0');
const firstDayOfNextMonth = `{year}-{month}-{day}`;
console.log(`下一个月的第一天:{firstDayOfNextMonth}`);
Output:
15. 获取当前日期的上一个月的最后一天
const now = new Date();
const nextDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
const year = nextDay.getFullYear();
const month = String(nextDay.getMonth() + 1).padStart(2, '0');
const day = String(nextDay.getDate()).padStart(2, '0');
const nextDayDate = `{year}-{month}-{day}`;
console.log(`明天的日期:{nextDayDate}`);
Output:
16. 获取当前日期的上一个月的最后一天
const now = new Date();
const lastDayOfLastMonth = new Date(now.getFullYear(), now.getMonth(), 0);
const year = lastDayOfLastMonth.getFullYear();
const month = String(lastDayOfLastMonth.getMonth() + 1).padStart(2, '0');
const day = String(lastDayOfLastMonth.getDate()).padStart(2, '0');
const lastDayDate = `{year}-{month}-{day}`;
console.log(`上一个月的最后一天:{lastDayDate}`);
Output:
17. 获取当前日期所在周的第一天和最后一天
const now = new Date();
const firstDayOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay());
const lastDayOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (6 - now.getDay()));
const year1 = firstDayOfWeek.getFullYear();
const month1 = String(firstDayOfWeek.getMonth() + 1).padStart(2, '0');
const day1 = String(firstDayOfWeek.getDate()).padStart(2, '0');
const year2 = lastDayOfWeek.getFullYear();
const month2 = String(lastDayOfWeek.getMonth() + 1).padStart(2, '0');
const day2 = String(lastDayOfWeek.getDate()).padStart(2, '0');
const firstDay = `{year1}-{month1}-{day1}`;
const lastDay = `{year2}-{month2}-{day2}`;
console.log(`本周的第一天:{firstDay}`);
console.log(`本周的最后一天:{lastDay}`);
Output:
18. 获取当前日期所在月的第一天和最后一天
const now = new Date();
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
const year1 = firstDayOfMonth.getFullYear();
const month1 = String(firstDayOfMonth.getMonth() + 1).padStart(2, '0');
const day1 = String(firstDayOfMonth.getDate()).padStart(2, '0');
const year2 = lastDayOfMonth.getFullYear();
const month2 = String(lastDayOfMonth.getMonth() + 1).padStart(2, '0');
const day2 = String(lastDayOfMonth.getDate()).padStart(2, '0');
const firstDay = `{year1}-{month1}-{day1}`;
const lastDay = `{year2}-{month2}-{day2}`;
console.log(`本月的第一天:{firstDay}`);
console.log(`本月的最后一天:{lastDay}`);
Output:
19. 获取当前日期所在年的第一天和最后一天
const now = new Date();
const firstDayOfYear = new Date(now.getFullYear(), 0, 1);
const lastDayOfYear = new Date(now.getFullYear(), 11, 31);
const year1 = firstDayOfYear.getFullYear();
const month1 = String(firstDayOfYear.getMonth() + 1).padStart(2, '0');
const day1 = String(firstDayOfYear.getDate()).padStart(2, '0');
const year2 = lastDayOfYear.getFullYear();
const month2 = String(lastDayOfYear.getMonth() + 1).padStart(2, '0');
const day2 = String(lastDayOfYear.getDate()).padStart(2, '0');
const firstDay = `{year1}-{month1}-{day1}`;
const lastDay = `{year2}-{month2}-{day2}`;
console.log(`今年的第一天:{firstDay}`);
console.log(`今年的最后一天:{lastDay}`);
Output:
20. 获取当前日期所在季度的第一天和最后一天
const now = new Date();
const quarter = Math.floor(now.getMonth() / 3);
const firstDayOfQuarter = new Date(now.getFullYear(), quarter * 3, 1);
const lastDayOfQuarter = new Date(now.getFullYear(), quarter * 3 + 3, 0);
const year1 = firstDayOfQuarter.getFullYear();
const month1 = String(firstDayOfQuarter.getMonth() + 1).padStart(2, '0');
const day1 = String(firstDayOfQuarter.getDate()).padStart(2, '0');
const year2 = lastDayOfQuarter.getFullYear();
const month2 = String(lastDayOfQuarter.getMonth() + 1).padStart(2, '0');
const day2 = String(lastDayOfQuarter.getDate()).padStart(2, '0');
const firstDay = `{year1}-{month1}-{day1}`;
const lastDay = `{year2}-{month2}-{day2}`;
console.log(`本季度的第一天:{firstDay}`);
console.log(`本季度的最后一天:{lastDay}`);
Output:
以上就是关于如何使用JavaScript获取当前日期的详细介绍。