查找网站备案,北京代理记账公司排名前十强,世界互联网乌镇峰会,任县网站建设价格信息ES6版本
function countdown(endTime, includeSeconds true) {// 获取当前时间let now new Date();// 将传入的结束时间字符串转换为日期对象let endDateTime new Date(endTime);// 检查传入的时间字符串是否只包含日期#xff08;不包含时分秒#xff09;if (endTime.tr…ES6版本
function countdown(endTime, includeSeconds true) {// 获取当前时间let now new Date();// 将传入的结束时间字符串转换为日期对象let endDateTime new Date(endTime);// 检查传入的时间字符串是否只包含日期不包含时分秒if (endTime.trim().split( ).length 1) {// 如果只有日期则将时间设置为该日期的午夜00:00:00endDateTime.setHours(0, 0, 0, 0);}// 计算当前时间与结束时间的时间差单位毫秒let timeDifference endDateTime - now;// 计算剩余的天数、小时数、分钟数和秒数let days Math.floor(timeDifference / (24 * 3600 * 1000));let hours Math.floor((timeDifference % (24 * 3600 * 1000)) / (3600 * 1000));let minutes Math.floor((timeDifference % (3600 * 1000)) / (60 * 1000));let seconds Math.floor((timeDifference % (60 * 1000)) / 1000);// 构建倒计时字符串let countdownString ${days}天${hours}时${minutes}分;// 根据includeSeconds参数决定是否包含秒数if (includeSeconds) {countdownString ${seconds}秒;}return countdownString;
}// 示例用法
// countdown(2078-01-28); // 只有日期
// countdown(2078-01-28 12:30:00); // 日期和时间
// countdown(2078-01-28, false); // 只有日期不包含秒数ES5版本
function countdown(endTime, includeSeconds) {// 设置默认值如果includeSeconds未定义则默认为trueincludeSeconds typeof includeSeconds ! undefined ? includeSeconds : true;// 获取当前时间var now new Date();// 将传入的结束时间字符串转换为日期对象var endDateTime new Date(endTime);// 检查传入的时间字符串是否只包含日期不包含时分秒if (endTime.trim().split( ).length 1) {// 如果只有日期则将时间设置为该日期的午夜00:00:00endDateTime.setHours(0, 0, 0, 0);}// 计算当前时间与结束时间的时间差单位毫秒var timeDifference endDateTime - now;// 计算剩余的天数、小时数、分钟数和秒数var days Math.floor(timeDifference / (24 * 3600 * 1000));var hours Math.floor((timeDifference % (24 * 3600 * 1000)) / (3600 * 1000));var minutes Math.floor((timeDifference % (3600 * 1000)) / (60 * 1000));var seconds Math.floor((timeDifference % (60 * 1000)) / 1000);// 构建倒计时字符串var countdownString days 天 hours 时 minutes 分;// 根据includeSeconds参数决定是否包含秒数if (includeSeconds) {countdownString seconds 秒;}return countdownString;
}// 示例用法
// countdown(2078-01-28); // 只有日期
// countdown(2078-01-28 12:30:00); // 日期和时间
// countdown(2078-01-28, false); // 只有日期不包含秒数