協(xié)調(diào)世界時(shí),又稱世界標(biāo)準(zhǔn)時(shí)間或世界協(xié)調(diào)時(shí)間,簡(jiǎn)稱UTC(從英文「Coordinated Universal Time」/法文「Temps Universel Cordonné」而來(lái)),是最主要的世界時(shí)間標(biāo)準(zhǔn),其以原子時(shí)秒長(zhǎng)為基礎(chǔ),在時(shí)刻上盡量接近于格林尼治平時(shí)
北京時(shí)間,China Standard Time,中國(guó)標(biāo)準(zhǔn)時(shí)間。在時(shí)區(qū)劃分上,屬東八區(qū),比協(xié)調(diào)世界時(shí)早8小時(shí),記為UTC+8。
不過(guò)這個(gè)CST這個(gè)縮寫(xiě)比較糾結(jié)的是它可以同時(shí)代表四個(gè)不同的時(shí)間:
Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30
China Standard Time UT+8:00
Cuba Standard Time UT-4:00
插一個(gè)中國(guó)地區(qū) JS 客戶端時(shí)間和服務(wù)端時(shí)間不一致的問(wèn)題
總結(jié)就是,前后端去傳時(shí)間的時(shí)候,盡量都用 UTC 時(shí)間。
if ( !Date.prototype.toISOString ) { ( function() { function pad(number) { if ( number < 10 ) { return '0' + number; } return number; } Date.prototype.toISOString = function() { return this.getUTCFullYear() + '-' + pad( this.getUTCMonth() + 1 ) + '-' + pad( this.getUTCDate() ) + 'T' + pad( this.getUTCHours() ) + ':' + pad( this.getUTCMinutes() ) + ':' + pad( this.getUTCSeconds() ) + '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z'; }; }() ); }
通過(guò) Polyfill 我們就能知道 ISO 是怎么表示時(shí)間的,最主要的特征是最后一位是“Z”,然后表示的總是 UTC 時(shí)間。
.valueOf()
的功能和.getTime()
一樣。
該方法通常在 JavaScript 內(nèi)部被調(diào)用,而不是在代碼中顯式調(diào)用。什么意思?沒(méi)有 valueOf
,那么Date
的實(shí)例是不能進(jìn)行運(yùn)算的。
var obj = Object.create(null); obj + 1; // Uncaught TypeError: Cannot convert object to primitive value(…)
直接看這個(gè) API 的名字的時(shí)候,我以為會(huì)返回一個(gè) JSON 格式的字符串,但其實(shí)是這么一個(gè)東西
new Date().toJSON() // "2016-05-05T06:03:28.130Z"
其實(shí)是這么回事
JSON.stringify(new Date()) // ""2016-05-05T06:06:02.615Z""
那結(jié)果能夠被 parse 嗎?
JSON.parse(JSON.stringify(new Date())) // "2016-05-05T06:19:24.766Z" JSON.parse('"' + new Date().toJSON() + '"') // "2016-05-05T06:19:24.766Z"
但是結(jié)果只是字符串而已。需要再講這個(gè)字符串交給 new Date()
才行。
不屬于任何標(biāo)準(zhǔn)。在JavaScript 1.6中被實(shí)現(xiàn)。似乎也只有 Firefox 自持這個(gè) API,其實(shí)正確姿勢(shì)是用.toLocaleDateString()
.toLcale各種String(locales [, options]])
媽的這個(gè) API 有點(diǎn)煩,看 MDN 的文檔你就知道。這個(gè) API 是用來(lái)本地化時(shí)間的。
這里稍微說(shuō)下我對(duì)這些參數(shù)的理解:
locales
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // formats below assume the local time zone of the locale; // America/Los_Angeles for the US // US English uses month-day-year order alert(date.toLocaleString("en-US")); // → "12/19/2012, 7:00:00 PM" // British English uses day-month-year order alert(date.toLocaleString("en-GB")); // → "20/12/2012 03:00:00" // Korean uses year-month-day order alert(date.toLocaleString("ko-KR")); // → "2012. 12. 20. ?? 12:00:00" // Arabic in most Arabic speaking countries uses real Arabic digits alert(date.toLocaleString("ar-EG")); // → "???/???/???? ?:??:?? ?" // for Japanese, applications may want to use the Japanese calendar, // where 2012 was the year 24 of the Heisei era alert(date.toLocaleString("ja-JP-u-ca-japanese")); // → "24/12/20 12:00:00" // when requesting a language that may not be supported, such as // Balinese, include a fallback language, in this case Indonesian alert(date.toLocaleString(["ban", "id"])); // → "20/12/2012 11.00.00"
以locales
所指的地區(qū)的時(shí)區(qū)和語(yǔ)言輸出。
options
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
localeMatcher
選擇本地匹配的什么算法,似乎沒(méi)什么大用
timeZone
再設(shè)置下 UTC 時(shí)區(qū)
hour12
是否12小時(shí)制
formatMatcher
各日期時(shí)間單元的格式化
weekday
Possible values are "narrow", "short", "long"
.
era
Possible values are "narrow", "short", "long"
.
year
Possible values are "numeric", "2-digit"
.
month
Possible values are "numeric", "2-digit", "narrow", "short", "long"
.
day
Possible values are "numeric", "2-digit"
.
hour
Possible values are "numeric", "2-digit"
.
minute
Possible values are "numeric", "2-digit"
.
second
Possible values are "numeric", "2-digit"
.
timeZoneName
Possible values are "short", "long"
.
栗子:
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); date.toLocaleString("en-US", {hour12: false}); // "12/19/2012, 19:00:00" var options = {timeZoneName:'long',weekday: "long", year: "2-digit", month: "narrow", day: "numeric"}; date.toLocaleString("en-US", options); // "Thursday, D 20, 12, China Standard Time"
插一個(gè)JavaScript 顯示 Y-m-d H:i:s 的日期時(shí)間格式
let date = new Date(); let result = [ [ date.getFullYear(), date.getMonth() + 1, date.getDate() ].join('-'), [ date.getHours(), date.getMinutes(), date.getSeconds() ].join(':') ].join(' ').replace(/\b\d\b/g, '0$&');
var date = new Date(); var result = date.toLocaleString('zh-CN', { hour12: false }) .replace(/\//g, '-').replace(/\b\d\b/g, '0$&');
https://github.com/moment/moment
https://github.com/rmm5t/jquery-timeago
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com