Skip to content

new Date() 获取当前客户端本地的日期,结果是一个日期对象 客户端时间可以任意更改,不能拿它做重要的业务判断

new Date([时间字符串]) -> 把一个时间字符串变为标准的日期对象 yyyy/mm/dd hh/mm/ss yyyy/mm/dd ...

new Date(2026, 2, 9) 和 new Date(2026, 02, 09) 这种形式构造日期时,会返回本地时区时间,需要转为 UTC 时间 new Date(1776221740834) 这种形式构造日期时,返回的是 UTC 时间 new Date('2026-01-01') 这种形式构造日期时,返回的是 UTC 时间 new Date('2026-10-11') 这种形式构造日期时,返回的是 UTC 时间 new Date('2026-1-01') 这种形式构造日期时,返回的是本地时区时间,需要转为 UTC 时间 new Date('2026-01-1') 这种形式构造日期时,返回的是本地时区时间,需要转为 UTC 时间

js
const date = new Date(2026, 2, 11)
new Date(
  Date.UTC(
    date.getFullYear(),
    date.getMonth(),
    date.getDate()
  )
)

getFullYear 获取年 4位
let time = new Date() time.getFullYead() getMonth 获取月 0-11 代表 1-12月 getDate 获取日 getHours 获取小时 getMinutes 获取分 getSeconds 获取秒 getMilliseconds 获取毫秒 getDay 获取星期 0-6 代表周日-周六 getTime 距离1970-01-01 00:00:00之间的毫秒差