日期對象和字符串操做、字符串API

<script>
        // 日期對象,Data類型使用UTC1970年1月1日午夜(零時)開始通過的毫秒數來保存日期
        // 建立一個日期對象,
        // let time = new Date();   // 不傳參表示輸出當時的時間
        // console.log(time);
        // //建立特定日期
        // let time1 = new Date(1234567);   // 這個參數是一個毫秒值 從1970年1月1日00:00:00開始加上這個一個毫秒值
        // console.log(time1);      //Thu Jan 01 1970 08:20:34 GMT+0800
        // let time2 = new Date("January 6,2014");  //參數爲日期字符串
        // console.log(time2);      //Mon Jan 06 2014 00:00:00 GMT+0800
        // let time3 = new Date(2019, 5, 1, 19, 30, 50, 20);  // //參數爲多個整數包括:年 月 日 時 分 秒 毫秒
        // console.log(time3);      //Sat Jun 01 2019 19:30:50 GMT+0800
        // let time4 = new Date("2019-5-1");  //Wed May 01 2019 00:00:00 GMT+0800
        // console.log(time4);
        // let time5 = new Date("2019/5/1");   //Wed May 01 2019 00:00:00 GMT+0800
        // console.log(time5);

        // 日期對象計算
        // Date.now()方法,返回表示調用這個方法的日期和時間的毫秒數
        // let start = Date.now();
        // for(let i=0; i<10000; i++){
        //     console.log((1));
        // }
        // let end = Date.now();
        // console.log(end -start);  //804  執行完後的毫秒數
        // 如要求距離上個時間有多少天  (end -start) / 1000 / 60 /60 / 24


        // let time1 = Date.parse(2019,4,6);
        // // 接收一個日期字符串,返回從1970-1-1 00:00:00到該日期的毫秒數
        // console.log(time1);   //1546300800000
        // let time2 = Date.UTC(2019,4,6);
        // // 接收以逗號隔開的日期參數,返回從1970-1-1 00:00:00到該日期的毫秒數 接收月份0-11
        // console.log(time2);   //1546300800000

        // 日期對象格式化方法
        // let time = new Date();              
        // console.log(time.toDateString());   //Sat Jul 11 2020
        // //返回的是星期 月 日 年
        // console.log(time.toTimeString());   //23:07:20 GMT+0800 (中國標準時間)
        // //返回的是時 分 秒 時區
        // console.log(time.toLocaleDateString());  //2020/7/11
        // //返回的是年/月/日
        // console.log(time.toLocaleTimeString());   //下午11:10:34
        // // 返回本地時 分 秒
        // console.log(time.toUTCString());      //Sat, 11 Jul 2020 15:10:34 GMT
        // // 返回對應的UTC時間 也就是國際標準時間 比北京晚8個小時
        // console.log(time.toLocaleString());   //2020/7/11 下午11:10:34
        //返回本地時間

        // 日期對象的日期方法
        // getMonth() 返回月 注意:獲得的月份是從0開始 要返回當前月須要加1
        // getDate() 返回日期
        // getFullYear() 返回年
        // getHours() 返回小時
        // getMinutes() 返回分鐘
        // getSeconds() 返回秒
        // getDay() 返回星期
        // getTimezoneOffset() 返回是當前事件與UTC的時區差別 以分鐘數表示(考慮夏令營時)
        // getTime() 返回一個毫秒值  時間爲此刻到時間零點的時間

        // 字符串和字符串API
        // let str = "wer";    
        // let str1 = "hello";
        // console.log(str.length);  //3
        // // 返回字符串長度  只能讀不能改
        // console.log(str.charAt(1));  //e
        // //獲取字符串中得下標得值,只可獲取不可修改
        // console.log(str.concat(str1,"j"));  //werhelloj
        // //字符串拼接 concat或者+返回新得字符串
        // console.log(str.indexOf("r"));  //2
        // // 在字符串中查詢某字符是否存在,存在返回下標,不存在返回-1;返回第一次匹配的字符的下標
        // console.log(str1.lastIndexOf("l"));  //3
        // 返回最後一個匹配的字符的下標

        // 字符串裁切slice,
        // slice(開始裁切的位置,結束裁切的位置)
        // 若是隻有一個參數, 那麼默認把該參數設置爲開始位置, 一直裁切到字符串末尾
        // 若是參數是負數, 那麼就是倒過來數, 從數組的結尾開始數數. 
        // 例:設參數爲-a, 那麼開始位置的索引就是string.length-a
        // let str = "heihei1";
        // console.log(str.slice(3));   //hei1
        // console.log(str.slice(3,5));  //he
        // console.log(str.slice(-2));   //i1

        // 字符串切割split
        // 該方法能夠將一個字符串變爲一個數組
        // split(切割方法)
        // 若是傳入空值, 那麼就是把整個字符串都變成數組的一個數組項目
        // 若是傳入空字符串,那麼每個單個字符都變成數組的一個數組項目
        // 若是傳入其餘的字符,那麼以這個字符爲分割線,分隔字符串, 並把分隔後的每一小塊的字符變成數組的一個數組項目
        // let str = "heihei1";
        // console.log(str.split());    //["heihei1"]
        // console.log(str.split(""));  //["h", "e", "i", "h", "e", "i", "1"]
        // console.log(str.split("h"));  //["", "ei", "ei1"]

        // trim() 刪除字符串前面和後面得空格
        // let str = "  heihei1   ";
        // console.log(str);  //  heihei1  
        // console.log(str.trim()); //heihei1
        //註冊、登錄時

    </script>
相關文章
相關標籤/搜索