[特殊字符] JS Date 对象8大使用场景

📅 JS Date 对象8大使用场景

new Date() 是JS最常用、最实用的内置对象,专门处理时间、日期

基础获取、制定

js 复制代码
// 获取当前时间
const now = new Date();

// 指定时间
const date = new Date("2025-10-01");

开发必用的 10 个实用方法

获取 年 / 月 / 日 / 时 / 分 / 秒

js 复制代码
const date = new Date();

date.getFullYear();   // 年 → 2026
date.getMonth() + 1;  // 月 → 3(注意:月份从0开始!必须+1)
date.getDate();       // 日 → 23
date.getHours();      // 时
date.getMinutes();    // 分
date.getSeconds();    // 秒
date.getDay();        // 星期 0=周日,1=周一 ... 6=周六

时间格式化

js 复制代码
// 把 `2026-03-23T12:34:56.789Z` 变成 `2026-03-23 12:34:56`
function formatTime(date) {
  const y = date.getFullYear();
  const m = (date.getMonth() + 1).toString().padStart(2, '0');
  const d = date.getDate().toString().padStart(2, '0');
  const hh = date.getHours().toString().padStart(2, '0');
  const mm = date.getMinutes().toString().padStart(2, '0');
  const ss = date.getSeconds().toString().padStart(2, '0');
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
}

获取时间戳

js 复制代码
// 方式1
const ts = Date.now();

// 方式2
const ts2 = new Date().getTime();

console.log(ts); // 1742789012345
console.log(ts2); // 1742789012345

计算两个时间差

js 复制代码
const start = new Date("2026-03-23").getTime();
const end = new Date("2026-03-25").getTime();
const diff = end - start; // 毫秒差

const day = diff / (1000 * 60 * 60 * 24); // 天数

倒计时功能

js 复制代码
function countDown(endTime) {
  const now = Date.now();
  const diff = endTime - now;
  const sec = diff / 1000;

  const d = parseInt(sec / 3600 / 24);
  const h = parseInt((sec / 3600) % 24);
  const m = parseInt((sec / 60) % 60);
  const s = parseInt(sec % 60);

  return `${d}天 ${h}时 ${m}分 ${s}秒`;
}

判断是否是今天

js 复制代码
function isToday(date) {
  const today = new Date();
  return date.getDate() === today.getDate()
      && date.getMonth() === today.getMonth()
      && date.getFullYear() === today.getFullYear();
}

时间转刚刚 / 几分钟前 / 几小时前

js 复制代码
function timeAgo(timestamp) {
  const now = Date.now();
  const diff = (now - timestamp) / 1000;// /1000 ->转换为秒

  if (diff < 60) return '刚刚';
  if (diff < 3600) return parseInt(diff / 60) + '分钟前';
  if (diff < 86400) return parseInt(diff / 3600) + '小时前';
  return parseInt(diff / 86400) + '天前';
}

获取当月第一天 / 最后一天

js 复制代码
// 当月第一天
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);

// 当月最后一天
const lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
相关推荐
海兰18 分钟前
【应用】基于 Next.js 16 + Python mplfinance的金融K线图与技术指标可视化平台(三)
javascript·python·金融
EXI-小洲20 分钟前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
wangruofeng29 分钟前
Phosphor Icons 官网源码拆解:URL 即存储、水波动画与 7362 Star 图标库的架构实践
前端·架构·github
BigTopOne1 小时前
WebRTC Native / Android 开发学习资源整理
前端
会周易的程序员1 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导1 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan1 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251491 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
excel2 小时前
nuxt 3 升级 nuxt 4 报错之 hasInjectionContext
前端
小磊哥er2 小时前
深入解构Claude Code - 第 7 篇 · 连接外面的世界
javascript·ai编程