亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 web前端 js教程 JavaScript 日期對(duì)象備忘單

JavaScript 日期對(duì)象備忘單

Dec 01, 2024 pm 07:03 PM

JavaScript Date Object Cheatsheet

JavaScript 中的 Date 對(duì)象 用于處理日期和時(shí)間。它提供了創(chuàng)建、操作和格式化日期和時(shí)間值的方法。


創(chuàng)建日期

您可以通過多種方式創(chuàng)建 Date 對(duì)象:

  1. 當(dāng)前日期和時(shí)間
   const now = new Date();
   console.log(now); // Current date and time
  1. 具體日期
   const specificDate = new Date(2024, 10, 21); // Year, Month (0-based), Day
   console.log(specificDate); // Thu Nov 21 2024
  1. 來自字符串
   const fromString = new Date("2024-11-21T10:00:00");
   console.log(fromString); // Thu Nov 21 2024 10:00:00 GMT
  1. 來自時(shí)間戳(自 Unix 紀(jì)元以來的毫秒數(shù)):
   const fromTimestamp = new Date(1732231200000);
   console.log(fromTimestamp); // Thu Nov 21 2024 10:00:00 GMT

常用方法

獲取日期和時(shí)間

Method Description Example
getFullYear() Returns the year date.getFullYear() -> 2024
getMonth() Returns the month (0-11) date.getMonth() -> 10 (November)
getDate() Returns the day of the month (1-31) date.getDate() -> 21
getDay() Returns the weekday (0-6, Sun=0) date.getDay() -> 4 (Thursday)
getHours() Returns the hour (0-23) date.getHours() -> 10
getMinutes() Returns the minutes (0-59) date.getMinutes() -> 0
getSeconds() Returns the seconds (0-59) date.getSeconds() -> 0
getTime() Returns timestamp in milliseconds date.getTime() -> 1732231200000
方法
描述

示例 標(biāo)題> getFullYear() 返回年份 date.getFullYear() ->; 2024年 獲取月份() 返回月份(0-11) 日期.getMonth() -> 10(十一月) 獲取日期() 返回月份中的第幾天 (1-31) 日期.getDate() -> 21 getDay() 返回工作日(0-6,Sun=0) 日期.getDay() -> 4(星期四) getHours() 返回小時(shí) (0-23) 日期.getHours() -> 10 getMinutes() 返回分鐘 (0-59) date.getMinutes() ->; 0 getSeconds() 返回秒(0-59) date.getSeconds() ->; 0 獲取時(shí)間() 返回時(shí)間戳(以毫秒為單位) 日期.getTime() -> 1732231200000 表>
Method Description Example
setFullYear(year) Sets the year date.setFullYear(2025)
setMonth(month) Sets the month (0-11) date.setMonth(0) -> January
setDate(day) Sets the day of the month date.setDate(1) -> First day of the month
setHours(hour) Sets the hour (0-23) date.setHours(12)
setMinutes(minutes) Sets the minutes (0-59) date.setMinutes(30)
setSeconds(seconds) Sets the seconds (0-59) date.setSeconds(45)
設(shè)置日期和時(shí)間 方法 描述 示例 標(biāo)題> setFullYear(年) 設(shè)置年份 日期.setFullYear(2025) 設(shè)置月份(月) 設(shè)置月份(0-11) 日期.setMonth(0) ->一月 設(shè)置日期(天) 設(shè)置月份中的日期 日期.setDate(1) ->該月的第一天 setHours(小時(shí)) 設(shè)置小時(shí)(0-23) 日期.setHours(12) setMinutes(分鐘) 設(shè)置分鐘(0-59) 日期.setMinutes(30) setSeconds(秒) 設(shè)置秒(0-59) 日期.setSeconds(45) 表>

格式化日期

Method Description Example
toDateString() Returns date as a human-readable string date.toDateString() -> "Thu Nov 21 2024"
toISOString() Returns date in ISO format date.toISOString() -> "2024-11-21T10:00:00.000Z"
toLocaleDateString() Returns date in localized format date.toLocaleDateString() -> "11/21/2024"
toLocaleTimeString() Returns time in localized format date.toLocaleTimeString() -> "10:00:00 AM"
方法
描述

示例 標(biāo)題> toDateString() 以人類可讀的字符串形式返回日期 date.toDateString() ->; “2024 年 11 月 21 日星期四” toISOString() 返回 ISO 格式的日期 date.toISOString() ->; “2024-11-21T10:00:00.000Z” toLocaleDateString() 以本地化格式返回日期 date.toLocaleDateString() ->; “2024 年 11 月 21 日” toLocaleTimeString() 以本地化格式返回時(shí)間 date.toLocaleTimeString() ->; “上午 10:00:00” 表>
  1. 常見用例
   const now = new Date();
   console.log(now); // Current date and time
    計(jì)算兩個(gè)日期之間的天數(shù)
   const specificDate = new Date(2024, 10, 21); // Year, Month (0-based), Day
   console.log(specificDate); // Thu Nov 21 2024
    倒計(jì)時(shí)器
   const fromString = new Date("2024-11-21T10:00:00");
   console.log(fromString); // Thu Nov 21 2024 10:00:00 GMT
    格式化當(dāng)前日期
   const fromTimestamp = new Date(1732231200000);
   console.log(fromTimestamp); // Thu Nov 21 2024 10:00:00 GMT
    查找一周中的哪一天
   const startDate = new Date("2024-11-01");
   const endDate = new Date("2024-11-21");
   const diffInTime = endDate - startDate; // Difference in milliseconds
   const diffInDays = diffInTime / (1000 * 60 * 60 * 24); // Convert to days
   console.log(diffInDays); // 20
    檢查閏年
   const targetDate = new Date("2024-12-31T23:59:59");
   setInterval(() => {
       const now = new Date();
       const timeLeft = targetDate - now; // Milliseconds left
       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);

加/減天數(shù)

  1. 專業(yè)提示
   const now = new Date();
   const formatted = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
   console.log(formatted); // "2024-11-21"
使用
    Date.now()
  1. 直接獲取當(dāng)前時(shí)間戳,無需創(chuàng)建 Date 對(duì)象:

    處理跨區(qū)域的日期時(shí)請(qǐng)注意時(shí)區(qū)

    。使用
  2. Moment.js
  3. Day.js 等庫進(jìn)行高級(jí)處理。

為了避免月份相差一的錯(cuò)誤,請(qǐng)記住月份是
0 索引
(0 = 一月,11 = 十二月)。

以上是JavaScript 日期對(duì)象備忘單的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
如何在node.js中提出HTTP請(qǐng)求? 如何在node.js中提出HTTP請(qǐng)求? Jul 13, 2025 am 02:18 AM

在Node.js中發(fā)起HTTP請(qǐng)求有三種常用方式:使用內(nèi)置模塊、axios和node-fetch。1.使用內(nèi)置的http/https模塊無需依賴,適合基礎(chǔ)場景,但需手動(dòng)處理數(shù)據(jù)拼接和錯(cuò)誤監(jiān)聽,例如用https.get()獲取數(shù)據(jù)或通過.write()發(fā)送POST請(qǐng)求;2.axios是基于Promise的第三方庫,語法簡潔且功能強(qiáng)大,支持async/await、自動(dòng)JSON轉(zhuǎn)換、攔截器等,推薦用于簡化異步請(qǐng)求操作;3.node-fetch提供類似瀏覽器fetch的風(fēng)格,基于Promise且語法簡單

JavaScript數(shù)據(jù)類型:原始與參考 JavaScript數(shù)據(jù)類型:原始與參考 Jul 13, 2025 am 02:43 AM

JavaScript的數(shù)據(jù)類型分為原始類型和引用類型。原始類型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時(shí)復(fù)制副本,因此互不影響;引用類型如對(duì)象、數(shù)組和函數(shù)存儲(chǔ)的是內(nèi)存地址,指向同一對(duì)象的變量會(huì)相互影響。判斷類型可用typeof和instanceof,但需注意typeofnull的歷史問題。理解這兩類差異有助于編寫更穩(wěn)定可靠的代碼。

React與Angular vs Vue:哪個(gè)JS框架最好? React與Angular vs Vue:哪個(gè)JS框架最好? Jul 05, 2025 am 02:24 AM

選哪個(gè)JavaScript框架最好?答案是根據(jù)需求選擇最適合的。1.React靈活自由,適合需要高度定制、團(tuán)隊(duì)有架構(gòu)能力的中大型項(xiàng)目;2.Angular提供完整解決方案,適合企業(yè)級(jí)應(yīng)用和長期維護(hù)的大項(xiàng)目;3.Vue上手簡單,適合中小型項(xiàng)目或快速開發(fā)。此外,是否已有技術(shù)棧、團(tuán)隊(duì)規(guī)模、項(xiàng)目生命周期及是否需要SSR也都是選擇框架的重要因素。總之,沒有絕對(duì)最好的框架,適合自己需求的就是最佳選擇。

JavaScript時(shí)間對(duì)象,某人構(gòu)建了一個(gè)eactexe,在Google Chrome上更快的網(wǎng)站等等 JavaScript時(shí)間對(duì)象,某人構(gòu)建了一個(gè)eactexe,在Google Chrome上更快的網(wǎng)站等等 Jul 08, 2025 pm 02:27 PM

JavaScript開發(fā)者們,大家好!歡迎閱讀本周的JavaScript新聞!本周我們將重點(diǎn)關(guān)注:Oracle與Deno的商標(biāo)糾紛、新的JavaScript時(shí)間對(duì)象獲得瀏覽器支持、GoogleChrome的更新以及一些強(qiáng)大的開發(fā)者工具。讓我們開始吧!Oracle與Deno的商標(biāo)之爭Oracle試圖注冊(cè)“JavaScript”商標(biāo)的舉動(dòng)引發(fā)爭議。Node.js和Deno的創(chuàng)建者RyanDahl已提交請(qǐng)?jiān)笗?,要求取消該商?biāo),他認(rèn)為JavaScript是一個(gè)開放標(biāo)準(zhǔn),不應(yīng)由Oracle

處理諾言:鏈接,錯(cuò)誤處理和承諾在JavaScript中 處理諾言:鏈接,錯(cuò)誤處理和承諾在JavaScript中 Jul 08, 2025 am 02:40 AM

Promise是JavaScript中處理異步操作的核心機(jī)制,理解鏈?zhǔn)秸{(diào)用、錯(cuò)誤處理和組合器是掌握其應(yīng)用的關(guān)鍵。1.鏈?zhǔn)秸{(diào)用通過.then()返回新Promise實(shí)現(xiàn)異步流程串聯(lián),每個(gè).then()接收上一步結(jié)果并可返回值或Promise;2.錯(cuò)誤處理應(yīng)統(tǒng)一使用.catch()捕獲異常,避免靜默失敗,并可在catch中返回默認(rèn)值繼續(xù)流程;3.組合器如Promise.all()(全成功才成功)、Promise.race()(首個(gè)完成即返回)和Promise.allSettled()(等待所有完成)

什么是緩存API?如何與服務(wù)人員使用? 什么是緩存API?如何與服務(wù)人員使用? Jul 08, 2025 am 02:43 AM

CacheAPI是瀏覽器提供的一種緩存網(wǎng)絡(luò)請(qǐng)求的工具,常與ServiceWorker配合使用,以提升網(wǎng)站性能和離線體驗(yàn)。1.它允許開發(fā)者手動(dòng)存儲(chǔ)如腳本、樣式表、圖片等資源;2.可根據(jù)請(qǐng)求匹配緩存響應(yīng);3.支持刪除特定緩存或清空整個(gè)緩存;4.通過ServiceWorker監(jiān)聽fetch事件實(shí)現(xiàn)緩存優(yōu)先或網(wǎng)絡(luò)優(yōu)先等策略;5.常用于離線支持、加快重復(fù)訪問速度、預(yù)加載關(guān)鍵資源及后臺(tái)更新內(nèi)容;6.使用時(shí)需注意緩存版本控制、存儲(chǔ)限制及與HTTP緩存機(jī)制的區(qū)別。

利用Array.Prototype方法用于JavaScript中的數(shù)據(jù)操作 利用Array.Prototype方法用于JavaScript中的數(shù)據(jù)操作 Jul 06, 2025 am 02:36 AM

JavaScript數(shù)組內(nèi)置方法如.map()、.filter()和.reduce()可簡化數(shù)據(jù)處理;1).map()用于一對(duì)一轉(zhuǎn)換元素生成新數(shù)組;2).filter()按條件篩選元素;3).reduce()用于聚合數(shù)據(jù)為單一值;使用時(shí)應(yīng)避免誤用導(dǎo)致副作用或性能問題。

JS綜述:深入研究JavaScript事件循環(huán) JS綜述:深入研究JavaScript事件循環(huán) Jul 08, 2025 am 02:24 AM

JavaScript的事件循環(huán)通過協(xié)調(diào)調(diào)用棧、WebAPI和任務(wù)隊(duì)列來管理異步操作。1.調(diào)用棧執(zhí)行同步代碼,遇到異步任務(wù)時(shí)交由WebAPI處理;2.WebAPI在后臺(tái)完成任務(wù)后將回調(diào)放入相應(yīng)的隊(duì)列(宏任務(wù)或微任務(wù));3.事件循環(huán)檢查調(diào)用棧是否為空,若為空則從隊(duì)列中取出回調(diào)推入調(diào)用棧執(zhí)行;4.微任務(wù)(如Promise.then)優(yōu)先于宏任務(wù)(如setTimeout)執(zhí)行;5.理解事件循環(huán)有助于避免阻塞主線程并優(yōu)化代碼執(zhí)行順序。

See all articles