Time and date format conversion Daquan

 Record the commonly used time conversion: encapsulate the formatted date and time into a method, and call it separately when needed

1. Convert 2023.06.13 to 2023-06-13

// format time

      getNowFormatDate(date) {

        //   let date = new Date(),

        let year = date.getFullYear(), //Get the full year (4 digits)

          month = date.getMonth() + 1, // Get the current month (0-11, 0 represents January)

          strDate = date.getDate(); // Get the current day (1-31)

        if (month < 10) month = `0${month}`; // If the month is a single digit, add 0 in front

        if (strDate < 10) strDate = `0${strDate}`; // If the day is a single digit, add 0 in front

        return `${year}-${month}-${strDate}`;

      },

2. Convert the timestamp to 2023-06-13 11:17:23

formattedDate(timestamp){
   const date = new Date(timestamp);
   const year = date.getFullYear();  
   const month = (date.getMonth() + 1).toString().padStart(2, '0');  
   const day = date.getDate().toString().padStart(2, '0'); 
   const hour = date.getHours().toString().padStart(2, '0');
   const minute = date.getMinutes().toString().padStart(2, '0');
   const second = date.getSeconds().toString().padStart(2, '0');
   const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`;  
   return formattedDate
 }
 

// Suppose there is a timestamp variable timestamp  
   const timestamp = 1615957883000;
   const newDate = this.formattedDate(timestamp)
   console.log(newDate); //The printed result is 2023-06-13 11:17:23
 

 3. Encapsulate the formatted week into a method, which can be called separately when needed

 getWeek(time){   // Define the abbreviation of the week     const show_day = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; const now = new   Date (time); // Get the current time    const dayOfWeek = now.getDay(); // Get the day of the week    return show_day[dayOfWeek]; }






  console.log(show_day[dayOfWeek]); //Tuesday

4. Date time string conversion format, for example:

20230304162053 converted to 2023-03-04 16:20:53

const date = '20230304162053'
const newDate = date.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, '$1-$2-$3 $4:$5:$6');
console.log(newDate);

5. Encapsulate the time conversion function and convert according to the current timestamp. And according to the parameter today, it returns the start time and end time of today, the parameter week returns the start time and end time of this week, and the parameter month returns the start time and end time of this month

function getTimeRange(type) {
  function getCurrentWeekRange() {
    const currentDate = new Date();

    // 获取本周的第一天,星期一为一周的起始
    const start = new Date(
      currentDate.getFullYear(),
      currentDate.getMonth(),
      currentDate.getDate() - currentDate.getDay() + 1
    );

    // 获取本周的最后一天,星期日为一周的结束
    const end = new Date(
      currentDate.getFullYear(),
      currentDate.getMonth(),
      currentDate.getDate() + (7 - currentDate.getDay())
    );

    return {
      start: formatDate(start),
      end: formatDate(end)
    };
  }

  function getCurrentMonthRange() {
    const currentDate = new Date();

    // 获取本月的第一天
    const start = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);

    // 获取下个月的第一天
    const nextMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);

    // 获取本月的最后一天
    const end = new Date(nextMonth - 1);

    return {
      start: formatDate(start),
      end: formatDate(end)
    };
  }

  function getCurrentDayRange() {
    const currentDate = new Date();

    // 获取今日的开始时间
    const start = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());

    // 获取今日的结束时间
    const end = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + 1);

    return {
      start: formatDate(start),
      end: formatDate(end)
    };
  }

  function formatDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return [year, month, day].join('-');
  }

  if (type === 'week') {
    return getCurrentWeekRange();
  } else if (type === 'month') {
    return getCurrentMonthRange();
  } else if (type === 'today') {
    return getCurrentDayRange();
  } else {
    return null;
  }
}
// getTimeRange函数的使用示例
const weekRange = getTimeRange('week');
const monthRange = getTimeRange('month');
const todayRange = getTimeRange('today');

//本周
console.log("本周开始时间:", weekRange.start); //2023-08-07
console.log("本周结束时间:", weekRange.end); //2023-08-13

//本月
console.log("本月开始时间:", monthRange.start); //2023-08-01
console.log("本月结束时间:", monthRange.end); //2023-08-31

//今日
console.log("今日开始时间:", todayRange.start); //2023-08-07
console.log("今日结束时间:", todayRange.end);  //2023-08-07

 

The timestamp before formatting must be a number, not a string, otherwise NaN will appear 

Guess you like

Origin blog.csdn.net/ll123456789_/article/details/131184421