当前位置:网站首页>Get the week start time and week end time of the current date

Get the week start time and week end time of the current date

2022-07-07 22:18:00 Belden wind

Native js Get the start time and end time of the week

/** *  Get the week start time and week end time of the current date  * @example * getWeekDate(new Date()) =>{ weekStartDate: new Date(), weekEndDate: new Date() } */
export const getWeekDate = (date: Date, format?: (date: Date) => string) => {
    
    const nowDayOfWeek = date.getDay() - 1; // Today is the third day of the week 
    const nowDay = date.getDate(); // Current day 
    const nowMonth = date.getMonth(); // The current month 
    const nowYear = date.getFullYear(); // The current year 
    const weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    const weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    return {
    
        weekStartDate: format ? format(weekStartDate) : weekStartDate,
        weekEndDate: format ? format(weekEndDate) : weekEndDate,
    };
};

原网站

版权声明
本文为[Belden wind]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130607022517.html