~~~
/* 時間字符串格式化:把一個時間日期字符串轉化為我們想要的格式 */
// =>"2020年03月11日 14時10分00秒"
// let time = '2020/3/11 14:10:0';
/* 1.把原始字符串中代表時間的值都獲取到,最后拼接成為我們想要的即可 */
/* let arr = time.split(' '); //=>["2020/3/11", "14:10:0"]
let arrLeft = arr[0].split('/'); //=>["2020", "3", "11"]
let arrRight = arr[1].split(':'); //=>["14", "10", "0"]
// 在拼接之前,需要把ARRLEFT和ARRRIGHT中不足兩位的數字,前面補充零
arrLeft = arrLeft.map(function (item) {
return item.length < 2 ? '0' + item : item;
});
arrRight = arrRight.map(item => item.length < 2 ? '0' + item : item);
time = `${arrLeft[0]}年${arrLeft[1]}月${arrLeft[2]}日 ${arrRight[0]}時${arrRight[1]}分${arrRight[2]}秒`;
console.log(time); */
/* let arr = time.split(' '); //=>["2020/3/11", "14:10:0"]
let arrLeft = arr[0].split('/'); //=>["2020", "3", "11"]
let arrRight = arr[1].split(':'); //=>["14", "10", "0"]
arr = arrLeft.concat(arrRight); //=>["2020", "3", "11", "14", "10", "0"]
arr = arr.map(item => item.length < 2 ? '0' + item : item);
time = `${arr[0]}年${arr[1]}月${arr[2]}日 ${arr[3]}時${arr[4]}分${arr[5]}秒`;
console.log(time); */
/* let time = '2020/3/11 14:10:0';
// let arr = time.split(/(?: |\/|:)/g); //=>["2020", "3", "11", "14", "10", "0"]
let arr = time.match(/\d+/g); //=>["2020", "3", "11", "14", "10", "0"]
arr = arr.map(item => item.length < 2 ? '0' + item : item);
time = `${arr[0]}年${arr[1]}月${arr[2]}日 ${arr[3]}時${arr[4]}分${arr[5]}秒`;
console.log(time); */
// 不足十位補充零的操作封裝為一個方法
/* function zero(val) {
return val.length < 2 ? '0' + val : val;
}
let time = '2020/3/11 14:10:0';
let arr = time.split(/(?: |\/|:)/g); //=>["2020", "3", "11", "14", "10", "0"]
time = `${arr[0]}年${zero(arr[1])}月${zero(arr[2])}日 ${zero(arr[3])}時${zero(arr[4])}分${zero(arr[5])}秒`;
console.log(time); */
/* 2.直接基于replace替換 */
/* let time = '2020/3/11 14:10:0';
time = time.replace('/', '年').replace('/', '月').replace(' ', '日 ').replace(':', '時').replace(':', '分') + '秒';
console.log(time); //=>沒有實現不足十位數字的補充零 */
/* 3.直接正則處理 */
// 封裝一個公共的,萬能的時間格式化的方法(此方法暫時不需要你們會,正式課咱們還會講)
/* (proto => {
function formatTime(template = '{0}年{1}月{2}日 {3}時{4}分{5}秒') {
let arr = this.match(/\d+/g);
return template.replace(/\{(\d+)\}/g, (_, n) => {
let item = arr[n] || '0';
item.length < 2 ? item = '0' + item : null;
return item;
});
}
proto.formatTime = formatTime;
})(String.prototype);
let time = '2020-3-11 14:10:0';
console.log(time.formatTime());
console.log(time.formatTime('{1}-{2} {3}:{4}'));
console.log(time.formatTime('{0}年{1}月{2}日')); */
~~~
- 0001.開課說明
- 0002.ECMAScript的發展歷程
- 0003.WEB2.0時代-服務器端渲染,前后端不分離
- 0004.WEB2.0時代-前后端分離模式
- 0005.大前端時代概述
- 0006.前端需要的技術棧和學習技巧
- 0007.瀏覽器
- 0008.JS的三部分組成
- 0009.JS中創建變量的6種形式
- 0010.JS中變量的命名規范
- 0011.JS中的數據類型分類
- 0012.JS中常用的幾種輸出方式
- 0013.number屬性類型詳細解讀1
- 0014.number數據類型詳細解讀2
- 0015.string數據類型詳細解讀1
- 0016.string數據類型詳細解讀2
- 0017.boolean數據類型詳細解讀
- 0018.object數據類型詳細解讀1
- 0019.object數據類型詳細解讀2
- 0020.談談學習
- 0021.數據類型檢測
- 0022.瀏覽器底層渲染機制(堆棧內存和數據類型區別)
- 0023.關于數據類型區別的面試題
- 0024.課后作業講解:數據類型轉換
- 0025.課后作業講解:堆棧內存處理
- 0026.課后作業講解:阿里的一道經典面試題
- 0027.JS中三種常用的判斷語句
- 0028.小實戰:開關燈特效
- 0029.FOR循環和FOR IN循環
- 0030.課后作業講解:關于循環判斷和數據轉化
- 0031.課后作業講解:關于DOM對象的深入理解
- 0032.關于元素集合的相關操作(奇偶行變色)
- 0033.課后作業講解:邏輯思維判斷題
- reset.min.css
- 0034.(復習)前四天內容的綜合復習梳理
- 0035.初窺函數:函數的作用、語法、形參
- 0036-0038.選項卡案例
- 0039.隔行變色案例:進一步強化自定義屬性編程思想
- 0040.其它作業題的講解(自定義屬性強化)
- 0041.函數創建和執行的堆棧運行機制
- 0042.函數中的形參和實參
- 0043.函數中的實參集合ARGUMENTS
- 0044.函數中的返回值RETURN
- 0045.箭頭函數和匿名函數
- 0046.兩個等于比較時候的數據類型轉換規則
- 0047.數組的基礎結構和常規操作
- 0048.數組常用方法:增刪改的五個方法
- 0049.數組常用方法:查詢、拼接、轉換為字符串
- 0050.數組常用方法:檢測是否包含、排序和迭代
- 0051.數組去重:雙FOR循環(數組塌陷和SPLICE刪除優化)
- 0052.數組去重:對象鍵值對方式(ES6中SET)
- 0053.Math數學函數對象中常用的方法
- 0054.String字符串中常用的方法
- 0055.實戰案例:時間字符串格式化
- 0056.實戰案例:queryURLParams1
- 0057.實戰案例:queryURLParams2
- 0058.實戰案例:獲取四位不重復的驗證碼
- 0059.階段作業題講解1(基礎知識)
- 0060.階段作業題講解2(實戰案例)
- 0061-0062.DOM操作中相關知識的復習
- 0063.DOM中的節點操作1
- 0064.DOM中的節點操作2
- utils
- 65.關于DOM的增刪改