uni-app location-related notes/get the timestamps at zero o'clock and 12 o'clock on the day

function getDayTimestamps(offset) {
  const currentDate = new Date();
  
  const targetDate = new Date(currentDate);
  targetDate.setDate(currentDate.getDate() - offset);
  
  targetDate.setHours(0, 0, 0, 0);
  const startTime = targetDate.getTime();

  targetDate.setHours(23, 59, 59, 999);
  const endTime = targetDate.getTime();
  
  return {
    startTime,
    endTime
  };
}

const yesterday = getDayTimestamps(1);
const twoDaysAgo = getDayTimestamps(2);

console.log("昨天开始时间戳:", yesterday.startTime);
console.log("昨天结束时间戳:", yesterday.endTime);
console.log("前两天开始时间戳:", twoDaysAgo.startTime);
console.log("前两天结束时间戳:", twoDaysAgo.endTime);

Guess you like

Origin blog.csdn.net/m0_73358221/article/details/131768428