JavaScript 中的 Date 对象用于处理日期和时间。它提供了创建、操作和格式化日期和时间值的方法。
创建日期 
你可以通过多种方式创建 Date 对象:
当前日期和时间: 
1 2 const  now = new  Date ();console .log (now); 
 
具体日期: 
1 2 const  specificDate = new  Date (2019 , 10 , 21 ); console .log (specificDate); 
 
来自字符串: 
1 2 const  fromString = new  Date ("2019-11-21T10:00:00" );console .log (fromString); 
 
来自时间戳(自 Unix 纪元以来的毫秒数): 
1 2 const  fromTimestamp = new  Date (1574899200000 );console .log (fromTimestamp); 
 
常用方法 
获取日期和时间 
方法 
描述 
示例 
 
 
getFullYear() 
返回年份 
date.getFullYear() -> 2019 
 
getMonth() 
返回月份 (0-11) 
date.getMonth() -> 10 (十一月) 
 
getDate() 
返回月份中的日期 (1-31) 
date.getDate() -> 21 
 
getDay() 
返回星期几 (0-6,星期日=0) 
date.getDay() -> 4 (星期四) 
 
getHours() 
返回小时 (0-23) 
date.getHours() -> 10 
 
getMinutes() 
返回分钟 (0-59) 
date.getMinutes() -> 0 
 
getSeconds() 
返回秒 (0-59) 
date.getSeconds() -> 0 
 
getTime() 
返回以毫秒为单位的时间戳 
date.getTime() -> 1732231200000 
 
 
设置日期和时间 
方法 
描述 
示例 
 
 
setFullYear(year) 
设置年份 
date.setFullYear(2025) 
 
setMonth(month) 
设置月份 (0-11) 
date.setMonth(0) -> 一月 
 
setDate(day) 
设置月份中的日期 
date.setDate(1) -> 月份中的第一天 
 
setHours(hour) 
设置小时 (0-23) 
date.setHours(12) 
 
setMinutes(minutes) 
设置分钟 (0-59) 
date.setMinutes(30) 
 
setSeconds(seconds) 
设置秒 (0-59) 
date.setSeconds(45) 
 
 
格式化日期 
方法 
描述 
示例 
 
 
toDateString() 
返回日期为人类可读的字符串 
date.toDateString() -> “Thu Nov 21 2019” 
 
toISOString() 
返回 ISO 格式的日期 
date.toISOString() -> “2019-11-21T10:00:00.000Z” 
 
toLocaleDateString() 
返回本地化格式的日期 
date.toLocaleDateString() -> “11/21/2019” 
 
toLocaleTimeString() 
返回本地化格式的时间 
date.toLocaleTimeString() -> “10:00:00 AM” 
 
 
常见用例 
计算两个日期之间的天数: 
1 2 3 4 5 const  startDate = new  Date ("2019-11-01" );const  endDate = new  Date ("2019-11-21" );const  diffInTime = endDate - startDate; const  diffInDays = diffInTime / (1000  * 60  * 60  * 24 ); console .log (diffInDays); 
 
倒计时器: 
1 2 3 4 5 6 7 8 9 10 const  targetDate = new  Date ("2019-12-31T23:59:59" );setInterval (() =>  {    const  now = new  Date ();     const  timeLeft = targetDate - now;      const  days = Math .floor (timeLeft / (1000  * 60  * 60  * 24 ));     const  hours = Math .floor ((timeLeft / (1000  * 60  * 60 )) % 24 );     const  minutes = Math .floor ((timeLeft / (1000  * 60 )) % 60 );     const  seconds = Math .floor ((timeLeft / 1000 ) % 60 );     console .log (`${days} d ${hours} h ${minutes} m ${seconds} s` ); }, 1000 ); 
 
格式化当前日期: 
1 2 3 const  now = new  Date ();const  formatted = `${now.getFullYear()} -${now.getMonth() + 1 } -${now.getDate()} ` ;console .log (formatted); 
 
查找星期几: 
1 2 3 4 5 6 7 8 9 10 11 const  date = new  Date ("2019-11-21" );const  days = [    "Sunday" ,     "Monday" ,     "Tuesday" ,     "Wednesday" ,     "Thursday" ,     "Friday" ,     "Saturday" , ]; console .log (days[date.getDay ()]); 
 
检查闰年: 
1 2 3 4 const  isLeapYear  = (year ) => {    return  (year % 4  === 0  && year % 100  !== 0 ) || year % 400  === 0 ; }; console .log (isLeapYear (2019 )); 
 
添加/减去天数: 
1 2 3 4 5 6 const  addDays  = (date, days ) => {    const  result = new  Date (date);     result.setDate (result.getDate () + days);     return  result; }; console .log (addDays (new  Date (), 10 )); 
 
专业提示 
使用 Date.now() 直接获取当前时间戳,而无需创建 Date 对象: 
 
1 console .log (Date .now ()); 
 
      
     
    
      
  
 
  
    
      
      
        
        致力于网站建设与Web开发。喜欢新事物,关注前后端动态,对新的技术有追求, 做一个优秀的web全栈工程师。