您的位置:首页 > 娱乐 > 八卦 > js中 获取上上周日和上周六的 转为年月日 的字符串

js中 获取上上周日和上周六的 转为年月日 的字符串

2024/10/6 2:21:44 来源:https://blog.csdn.net/qq_34874784/article/details/141887760  浏览:    关键词:js中 获取上上周日和上周六的 转为年月日 的字符串

在 Vue 中获取上上周日和上周六的日期,并将其转换为 "年月日" 格式的字符串,可以通过以下步骤实现:
方法一:使用 JavaScript 的 Date 对象
获取当前日期。
计算上上周日和上周六的日期。
格式化日期为 "年月日" 格式。
示例代码:

export default {name: 'ExampleComponent',methods: {getDates() {const today = new Date();// 获取今天的星期几const todayDayOfWeek = today.getDay(); // 0 表示周日,6 表示周六// 计算上周六的日期const lastSaturday = new Date(today);lastSaturday.setDate(today.getDate() - (todayDayOfWeek === 0 ? 6 : todayDayOfWeek));// 计算上上周日的日期const secondLastSunday = new Date(lastSaturday);secondLastSunday.setDate(lastSaturday.getDate() - 7);// 格式化日期const format = date => {const year = date.getFullYear();let month = date.getMonth() + 1; // 月份是从 0 开始的let day = date.getDate();if (month < 10) month = '0' + month;if (day < 10) day = '0' + day;return `${year}-${month}-${day}`;};const lastSaturdayFormatted = format(lastSaturday);const secondLastSundayFormatted = format(secondLastSunday);return {lastSaturday: lastSaturdayFormatted,secondLastSunday: secondLastSundayFormatted};}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com