一、日期时间
1.获取当前日期
代码如下:
function getCurrentDate() {
var currentDateTime = new Date(); // 创建一个当前日期时间的Date对象
var year = currentDateTime.getFullYear(); // 获取年份
var month = ('0' + (currentDateTime.getMonth() + 1)).slice(-2); // 获取月份(注意月份是从0开始计数)
var day = ('0' + currentDateTime.getDate()).slice(-2); // 获取日期
var formattedDate = year + '-' + month + '-' + day; // 格式化日期时间为"YYYY-MM-DD"的字符串格式
return formattedDate; // 返回格式化后的日期时间字符串
}
2.获取当前日期时间
代码如下:
function getCurrentDateTime() {
var currentDateTime = new Date(); // 创建一个当前日期时间的Date对象
var year = currentDateTime.getFullYear(); // 获取年份
var month = ('0' + (currentDateTime.getMonth() + 1)).slice(-2); // 获取月份(注意月份是从0开始计数)
var day = ('0' + currentDateTime.getDate()).slice(-2); // 获取日期
var hour = ('0' + currentDateTime.getHours()).slice(-2); // 获取小时
var minute = ('0' + currentDateTime.getMinutes()).slice(-2); // 获取分钟
var second = ('0' + currentDateTime.getSeconds()).slice(-2); // 获取秒
var formattedDateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; // 格式化日期时间为"YYYY-MM-DD HH:MM:SS"的字符串格式
return formattedDateTime; // 返回格式化后的日期时间字符串
}
3.格式化日期
代码如下:
/**
* 格式化日期
* @param {Date} date - 要格式化的日期对象
* @returns {string} - 格式化后的日期字符串,格式为 "YYYY-MM-DD"
*/
function formatDate(date) {
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = ('0' + date.getDate()).slice(-2);
return year + '-' + month + '-' + day;
}
4.格式化日期时间
代码如下:
/**
* 格式化日期时间
* @param {Date} date - 要格式化的日期对象
* @returns {string} - 格式化后的日期时间字符串,格式为 "YYYY-MM-DD HH:mm:ss"
*/
function formatDateTime(date) {
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = ('0' + date.getDate()).slice(-2);
var hour = ('0' + date.getHours()).slice(-2);
var minute = ('0' + date.getMinutes()).slice(-2);
var second = ('0' + date.getSeconds()).slice(-2);
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
5.将日期时间格式化为数字
代码如下:
function dateTimeToNumber(dateTime) {
// 分割日期和时间部分
var dateParts = dateTime.split(" ")[0].split("-");
var timeParts = dateTime.split(" ")[1].split(":");
// 提取日期部分的年、月、日
var year = parseInt(dateParts[0]);
var month = parseInt(dateParts[1]);
var day = parseInt(dateParts[2]);
// 提取时间部分的小时、分钟、秒钟
var hours = parseInt(timeParts[0]);
var minutes = parseInt(timeParts[1]);
var seconds = parseInt(timeParts[2]);
// 使用Date.UTC方法将日期时间部分转换为数字
var number = Date.UTC(year, month - 1, day, hours, minutes, seconds);
return number;
}
6.根据当前日期和自然天数计算结束日期
代码如下:
/**
* 根据当前日期和自然天数计算结束日期
* @param {Date} currentDate 当前日期
* @param {number} numDays 自然天数
* @returns {Date} 结束日期
*/
function calculateEndDate(currentDate, numDays) {
var endDate = new Date(currentDate); // 创建一个当前日期的副本,以免修改原始日期
endDate.setDate(endDate.getDate() + numDays); // 将当前日期增加自然天数
return endDate; // 返回计算得到的结束日期
}
// 示例用法
// var currentDate = new Date(); // 获取当前日期
// var numDays = 7; // 假设需要计算7天后的日期
// var endDate = calculateEndDate(currentDate, numDays); // 计算结束日期
// console.log("当前日期:" + currentDate.toISOString()); // 打印当前日期
// console.log("结束日期:" + endDate.toISOString()); // 打印结束日期
二、防抖节流
1.防抖
代码如下:
/**
* 防抖函数(延迟执行函数)
* @param {Function} func - 要防抖的函数
* @param {number} delay - 延迟时间间隔,单位毫秒
* @returns {Function} - 被防抖后的函数
*/
function debounce(func, delay) {
var timer = null;
return function () {
clearTimeout(timer);
timer = setTimeout(function () {
func.apply(this, arguments);
}, delay);
};
}
2.节流
代码如下:
/**
* 节流函数(限制函数执行频率)
* @param {Function} func - 要节流的函数
* @param {number} delay - 延迟时间间隔,单位毫秒
* @returns {Function} - 被节流后的函数
*/
function throttle(func, delay) {
var timer = null;
return function () {
if (!timer) {
timer = setTimeout(function () {
func.apply(this, arguments);
timer = null;
}, delay);
}
};
}