惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
F
Fortinet All Blogs
Y
Y Combinator Blog
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
S
Schneier on Security
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Simon Willison's Weblog
Simon Willison's Weblog
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
A
About on SuperTechFans
The Cloudflare Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
宝玉的分享
宝玉的分享
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
Martin Fowler
Martin Fowler
H
Help Net Security
B
Blog RSS Feed
B
Blog
爱范儿
爱范儿
V
V2EX
I
Intezer
L
LangChain Blog
WordPress大学
WordPress大学
小众软件
小众软件
美团技术团队
Latest news
Latest news
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tor Project blog
L
Lohrmann on Cybersecurity
Cyberwarzone
Cyberwarzone
Last Week in AI
Last Week in AI
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
V
Visual Studio Blog

CrazyWong

香港銀行開戶推薦 (附邀請碼) CrazyWong AI LOGO 🎧 Anson Seabra - Keep Your Head Up Princess 是的,我又更換了域名 1.5 萬字 CSS 基礎拾遺(核心知識、常見需求) JavaScript - 數組 Array 域名更換 前端 JavaScript 自測清單 2 前端 JavaScript 自測清單 1 通過travis-ci或者GitHub Actions自動化部署GitHub Pages和Coding Pages 添加Windows Terminal到鼠標右鍵菜單 調研實現高性能動畫 在瀏覽器輸入 URL 回車之後發生了什麼(超詳細版) hexo-theme-butterfly 安裝文檔(一)快速開始 設置Windows電腦自動關機 Windows必裝軟件推薦 關於字符編碼,你所需要知道的(ASCII,Unicode,Utf-8,GB2312…) 好用的新浪圖床工具推薦 - Weibo-Picture-Store Java知識點複習(二) Java知識點複習(一) Visual Studio Code 插件推薦-VSC Netease Music 重裝系統後重新部署恢復 Hexo blog Windows上Java的環境變量配置 Adapter 9Patch 介紹 第一行代碼筆記-RecyclerView 為Blog添加版權説明 第一行代碼筆記-ListView 第一行代碼筆記-創建自定義控件 第一行代碼筆記-四大Layout Hexo和Next主題的相關設置(持續更新) 第一行代碼筆記-bulid.gradle 解析 第一行代碼筆記-Android Studio工程目錄結構介紹 第一行代碼筆記-工具日志log
JavaScript - 日期 Date
MYW · 2021-03-11 · via CrazyWong

Date

Date 類型使用自 UTC 世界協調時間 1970 年 1 月 1 日午夜零時開始經過的毫秒數來保存日期

創建

通過使用 new 操作符和 Date 構造函數來創建日期對象

var now = new Date()

Date() 可以選擇傳入參數,如果不傳入參數

  1. 不傳入參數

    會依據系統設置的當前時間來創建一個Date對象,返回當前的日期和時間

  2. 傳入參數 Unix 时间戳

    即 从1970-1-1 00:00:00 UTC 到该日期對象(UTC時間)的毫秒数

  3. 傳入 时间戳字符串

    與下文的 Date.parse()所需的參數一樣

  4. 傳入 日期成員

    與 下文的 Date.UTC() 所需的參數一樣,但是 Date() 會以 本地時間 來處理參數,而不是 UTC。

    1
    2
    3

    console.log(new Date(Date.UTC(2021,1,2,12,12,12)))
    console.log(new Date(2021,1,2,12,12,12))

以一個函數的形式來調用 Date 對象(即不使用 new 操作符)會返回一個代表當前日期和時間的字符串

1
2
3
4
5
6
var d1 = new Date()
var d2 = Date()
console.log(d1)
console.log(d2)
console.log(typeof d1)
console.log(typeof d2)

獲取毫秒數方法

JavaScript 提供了兩種方法可以快速獲取毫秒數

  1. Date.parse()

    該方法接受一個表示日期的字符串參數(符合RFC 2822ISO 8601 的日期格式,其他格式也支持,但是結果可能會與預期不同),然後根據字符串返回相應的日期的毫秒數(从1970-1-1 00:00:00 UTC 到该日期對象(UTC時間)的毫秒数)。

    如果傳入的參數無法識別,則會返回 NaN

    如果參數沒有指定時區,默認使用本地時區。

    在 ISO 8601 格式中

    1. ‘2011-02-22’ 這種僅日期格式的,將會使用 UTC 時區來處理解析這個參數
    2. 如果是日期加時間的,則使用本地時間處理

    這個方法可能在不同瀏覽器和地區,有不一樣的結果

    1
    2
    3
    4
    5
    6
    7
    8
    console.log(Date.parse('2012-02-02')) 
    console.log(Date.parse('2012-02-02T02:02:02'))

    console.log(Date.parse("Feb 2, 2012"))
    console.log(Date.parse("Thu, 01 Jan 1970 00:00:00 GMT"))


    console.log(Date.parse('2012-02-42'))
  2. Date.UTC()

    Date.parse()一樣都是返回相應的日期的毫秒數(从1970-1-1 00:00:00 UTC 到该日期對象(UTC時間)的毫秒数),區別在於傳入的參數不同,其可以傳入很多參數。其傳入的參數會以 UTC 時區處理。

    Date.UTC(year,month[,date[,hrs[,min[,sec[,ms]]]]])

    • 【必須】year 年份

      可以是完整格式的年份,如 2022。

      如果是 0 到 99 之間,代表著 1900 年後的日期(傳入 8,會被渲染為 1908年)

    • 【必須】month 月份

      0 到 11 之間的整數

    • date 天數

      1 到 31 之間的整數

    • hrs 小時

      0 到 23 之間的整數

    • min 分鐘

      0 到 59 之間的整數

    • sec 秒

      0 到 59 之間的整數

    • ms 毫秒

      0 到 999 之間的整數

    注意: 如果沒有傳入 date 天數,則默認爲 1。

    而省略其它參數,這些參數都默認為 0

    如果有一個指定的參數超出其合理範圍,則 UTC 方法會通過更新其他參數直到該參數在合理範圍內。例如,為月份指定 15,則年份將會加 1,然後月份將會使用 3。

    1
    2
    console.log(Date.UTC(2021,1)) 
    console.log(Date.UTC(2021,1,2,12,12,12))

獲取當前時間

返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。

等同於 new Date().getTime()

1
2
console.log(new Date().getTime()) 
console.log(Date.now())

Getter 方法

方法解釋
getFullYear()根据本地时间返回指定日期对象的年份(四位数年份时返回四位数字)
getMonth()根据本地时间返回指定日期对象的月份(0-11)
getDate()根据本地时间返回指定日期对象的月份中的第几天(1-31)
getHours()根据本地时间返回指定日期对象的小时(0-23)
getMinutes()根据本地时间返回指定日期对象的分钟(0-59)
getSeconds()根据本地时间返回指定日期对象的秒数(0-59)
getMilliseconds()根据本地时间返回指定日期对象的毫秒(0-999)
getDay()根据本地时间返回指定日期对象的星期中的第几天(0-6)
0 表示 星期日
getTime()返回从1970-1-1 00:00:00 UTC(协调世界时)到该日期经过的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。
getTimezoneOffset()返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟
getUTCFullYear()根据世界时返回特定日期对象所在的年份(4位数)
getUTCMonth()根据世界时返回特定日期对象的月份(0-11)
0 代表 1 月,依此類推
getUTCDate()根据世界时返回特定日期对象一个月的第几天(1-31)
getUTCHours()根据世界时返回特定日期对象当前的小时(0-23)
getUTCMinutes()根据世界时返回特定日期对象的分钟数(0-59)
getUTCSeconds()根据世界时返回特定日期对象的秒数(0-59)
getUTCMilliseconds()根据世界时返回特定日期对象的毫秒数(0-999)
getUTCDay()根据世界时返回特定日期对象一个星期的第几天(0-6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const today = new Date(2021,2,10,22,55,21,44)

console.log(today)
console.log(today.getFullYear())
console.log(today.getMonth())
console.log(today.getDate())
console.log(today.getHours())
console.log(today.getMinutes())
console.log(today.getSeconds())
console.log(today.getMilliseconds())
console.log(today.getDay())
console.log(today.getTime())
console.log(today.getTimezoneOffset())

console.log(today.getUTCFullYear())
console.log(today.getUTCMonth())
console.log(today.getUTCDate())
console.log(today.getUTCHours())
console.log(today.getUTCMinutes())
console.log(today.getUTCSeconds())
console.log(today.getUTCMilliseconds())
console.log(today.getUTCDay())

Setter 方法

方法解釋
setDate()根据本地时间为指定的日期对象设置月份中的第几天
setFullYear()根据本地时间为指定日期对象设置完整年份(四位数年份是四个数字)
setMonth()根据本地时间为指定日期对象设置月份
setHours()根据本地时间为指定日期对象设置小时数
setMinutes()根据本地时间为指定日期对象设置分钟数
setSeconds()根据本地时间为指定日期对象设置秒数
setMilliseconds()根据本地时间为指定日期对象设置毫秒数
setTime()通过指定从 1970-1-1 00:00:00 UTC 开始经过的毫秒数来设置日期对象的时间,对于早于 1970-1-1 00:00:00 UTC的时间可使用负值
setUTCDate()根据世界时设置 Date 对象中月份的一天 (1 ~ 31)
setUTCFullYear()根据世界时设置 Date 对象中的年份(四位数字)
setUTCMonth()根据世界时设置 Date 对象中的月份 (0 ~ 11)
setUTCHours()根据世界时设置 Date 对象中的小时 (0 ~ 23)
setUTCMinutes()根据世界时设置 Date 对象中的分钟 (0 ~ 59)
setUTCSeconds()根据世界时设置 Date 对象中的秒钟 (0 ~ 59)
setUTCMilliseconds()根据世界时设置 Date 对象中的毫秒 (0 ~ 999)

Conversion 方法

方法解釋
toDateString()以美式英语和人类易读的形式返回该日期对象日期部分的字符串
toISOString()把一个日期转换为符合 ISO 8601 扩展格式的字符串。
格式:YYYY-MM-DDTHH:mm:ss.sssZ ,时区总是UTC
toJSON()調用 toISOString() 返回一个表示该日期的字符串。为了在 JSON.stringify() 方法中使用
toString()返回一个表示该日期对象的字符串。覆盖了Object.prototype.toString() 方法
toTimeString()以人类易读格式返回日期对象时间部分的字符串
toUTCString()把一个日期对象转换为一个以UTC时区计时的字符串
valueOf()返回一个日期对象的原始值。覆盖了 Object.prototype.valueOf() 方法
toLocaleString()返回一个表示该日期对象的字符串,该字符串与系统设置的地区关联(locality sensitive)。覆盖了 Object.prototype.toLocaleString() 方法
toLocaleTimeString()返回一个表示该日期对象时间部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)
toLocaleDateString()返回一个表示该日期对象日期部分的字符串,该字符串格式与系统设置的地区关联(locality sensitive)
1
2
3
4
5
6
7
8
9
const today = new Date(2021,2,10,22,55,21,44)
console.log(today)
console.log(today.toDateString());
console.log(today.toISOString());
console.log(today.toJSON());
console.log(today.toString());
console.log(today.toTimeString());
console.log(today.toUTCString());
console.log(today.valueOf());

toLocaleString() toLocaleTimeString()toLocaleDateString()

這3個方法都接受兩個參數

  • 【可選】locales

    指定語言代碼,例如 ‘zh-HK’

    具體語言代碼 可參考 MDN Intl 文檔

    當沒有指定語言代碼時,會返回一個使用運行時默認的語言環境和格式(options)的格式化字符串

  • 【可選】options

    指定日期時間等輸出(文字/格式 等)

    具體 options 可參考 MDN 文檔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var today = new Date(2021,2,10,22,55,21,44)
console.log(today);


console.log(today.toLocaleDateString());
console.log(today.toLocaleDateString('zh-HK'));
console.log(today.toLocaleDateString('en-US'));

var options = {weekday: "long", year: "numeric", month: "long", day: "numeric"};
console.log(today.toLocaleDateString("zh-HK", options));


console.log(today.toLocaleTimeString());
console.log(today.toLocaleTimeString('zh-HK'));
console.log(today.toLocaleTimeString('en-US'));
console.log(today.toLocaleTimeString('zh-HK',{hour12: false}));


console.log(today.toLocaleString());
console.log(today.toLocaleString('zh-HK'));
console.log(today.toLocaleString('en-US'));
console.log(today.toLocaleString('zh-HK',{hour12: false}));

參考

  1. 書籍 《JavaScript 高級程序設計》
  2. MDN 文檔

版權聲明: 本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 CrazyWong