js getday获取日期
1. 前言
在前端开发中,经常需要获取当前的日期以及对日期进行一些操作。JavaScript(简称JS)是一种流行的脚本语言,具有强大的日期处理能力。本文将详细介绍如何使用JS获取日期,并介绍常用的日期操作方法。
2. 获取当前日期
JS提供了Date()
对象来表示一个日期。要获取当前的日期,可以使用new Date()
构造函数,它会返回一个表示当前时间的Date
对象。
const currentDate = new Date();
console.log(currentDate);
运行结果:
2022-01-01T12:00:00.000Z
上述代码中,new Date()
构造函数返回的是当前时间的Date
对象。输出显示了日期和时间的详细信息。
如果只想获取当前的日期,可以使用Date
对象的一些方法。
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1;
const day = currentDate.getDate();
console.log(`{year}-{month}-${day}`);
运行结果:
2022-01-01
Date
对象的getFullYear()
方法返回当前年份,getMonth()
方法返回当前月份(0表示一月,11表示十二月),getDate()
方法返回当前日期。
3. 格式化日期
上述代码获取的日期格式为”年-月-日”,如果需要其他格式,可以使用Date
对象提供的方法进行格式化。
3.1. 格式化为”年-月-日 时:分:秒”
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1;
const day = currentDate.getDate();
const hour = currentDate.getHours();
const minute = currentDate.getMinutes();
const second = currentDate.getSeconds();
console.log(`{year}-{month}-{day}{hour}:{minute}:{second}`);
运行结果:
2022-01-01 12:00:00
Date
对象的getHours()
方法返回当前小时,getMinutes()
方法返回当前分钟,getSeconds()
方法返回当前秒数。
3.2. 自定义格式化函数
如果需要更灵活的日期格式,可以自定义一个格式化函数。
function formatDate(date) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
return `{year}-{month < 10 ? "0" + month : month}-${day < 10 ? "0" + day : day}`;
}
const currentDate = new Date();
console.log(formatDate(currentDate));
运行结果:
2022-01-01
上述代码定义了一个formatDate()
函数,接受一个Date
对象作为参数,并返回格式为”年-月-日”的日期字符串。如果月份或日期小于10,则在前面加上0。这样可以保证日期的一致性。
4. 日期计算
JS提供了一些日期计算的方法,可以在给定的日期上进行加、减操作。
4.1. 加减天数
要在给定的日期上加减特定的天数,可以使用Date
对象的setDate()
方法。
const currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 7);
console.log(currentDate);
运行结果:
2022-01-08T12:00:00.000Z
上述代码将当前日期加上7天,使用setDate()
方法设置修改日期。需要注意的是,setDate()
方法会修改Date
对象本身,而不是返回一个新的Date
对象。
同样的,可以通过减去特定的天数来获得之前的日期。
4.2. 加减月份
类似地,可以使用Date
对象的setMonth()
方法对日期进行加减月份的操作。
const currentDate = new Date();
currentDate.setMonth(currentDate.getMonth() + 1);
console.log(currentDate);
运行结果:
2022-02-01T12:00:00.000Z
上述代码将当前日期加上1个月。
4.3. 加减年份
同样地,可以使用Date
对象的setFullYear()
方法对日期进行加减年份的操作。
const currentDate = new Date();
currentDate.setFullYear(currentDate.getFullYear() + 1);
console.log(currentDate);
运行结果:
2023-01-01T12:00:00.000Z
上述代码将当前日期加上1年。
5. 其他常用方法
除了上述介绍的方法,JS的Date
对象还提供了一些其他常用的方法。
5.1. 获取星期几
可以使用Date
对象的getDay()
方法来获取当前日期是星期几。
const currentDate = new Date();
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const dayOfWeek = days[currentDate.getDay()];
console.log(dayOfWeek);
运行结果:
Saturday
getDay()
方法返回的是一个从0开始的数字,表示星期几。使用一个字符串数组来映射数字和星期名,可以方便地获取到星期几的名称。
5.2. 获取两个日期之间的天数差
要计算两个日期之间的天数差,可以先将日期转换成毫秒数,然后计算差值。
function getDaysDiff(date1, date2) {
const ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
const diffInMilliseconds = Math.abs(date1 - date2);
return Math.floor(diffInMilliseconds / ONE_DAY_IN_MILLISECONDS);
}
const date1 = new Date('2022-01-01');
const date2 = new Date('2022-01-08');
console.log(getDaysDiff(date1, date2));
运行结果:
7
上述代码将两个日期分别转换成毫秒数,计算差值,并通过除以一天的毫秒数得到天数差。
5.3. 判断闰年
可以使用Date
对象的getFullYear()
方法获取年份,然后判断是否为闰年。
function isLeapYear(year) {
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
return true;
}
return false;
}
console.log(isLeapYear(2000));
console.log(isLeapYear(2020));
console.log(isLeapYear(2022));
运行结果:
true
true
false
上述代码定义了一个isLeapYear()
函数,接受一个年份作为参数,并返回该年份是否为闰年。
6. 总结
本文详细介绍了如何使用JS获取当前日期,以及常用的日期操作方法。通过Date
对象的相关方法,可以方便地进行日期的格式化、计算和判断。在前端开发中,合理利用这些方法可以提高日期处理的效率和灵活性。