将YYYY-MM-DD HH:mm:ss格式化为YYYY-MM-DD (星期一) 下午 ?点

 分为凌晨、早上、中午、晚上

function formatDate(inputDate) {
  const date = new Date(inputDate);
  date.setHours(date.getHours() - 1);

  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份从0开始
  const day = date.getDate();
  let hours = date.getHours();
  const minutes = date.getMinutes();

  const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  const weekDay = weekDays[date.getDay()];

  let period;
  if (hours >= 0 && hours < 6) {
    period = '凌晨';
  } else if (hours >= 6 && hours < 12) {
    period = '早上';
  } else if (hours === 12) {
    period = '中午';
  } else if (hours > 12 && hours < 18) {
    period = '下午';
  } else {
    period = '晚上';
  }

  hours = hours % 12 || 12; // 转换为12小时制,0时为12时
  const formattedHours = hours.toString().padStart(2, '0');

  return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} (${weekDay}) ${period} ${formattedHours}点`;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值