<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          12種JS常用獲取時(shí)間的方式

          共 1446字,需瀏覽 3分鐘

           ·

          2020-11-11 22:48


          在編程中,總會(huì)遇到各種各樣的獲取時(shí)間的要求,下面我們來(lái)看一下獲取不同時(shí)間格式的方法有哪些?
          如果不記得的話建議收藏哦!

          1、獲取當(dāng)前的日期和時(shí)間

          方法:new Date()
          console.log(new Date())//Wed Nov 04 2020 18:20:49 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)

          2、獲取當(dāng)前日期

          可運(yùn)行代碼:

          console.log(new Date().toLocaleDateString())//2020/11/4

          不一樣的格式:

          function fn(){    let t = new Date()    let fn1 = (d) =>(‘0‘+d).substr(-2)    console.log(t.getFullYear()+‘-‘+    fn1(t.getMonth()+1)+‘-‘+    fn1(t.getDate()))    )}fn()//2020-11-04

          說(shuō)明:b.substr(-2)的含義是將字符串b字符串從后面數(shù)起,返回倒數(shù)兩位的字符

          3、返回當(dāng)前時(shí)間

          獲取的是12小時(shí)制:

          console.log(new Date().toLocaleTimeString())//下午6:23:09 

          獲取的是24小時(shí)制:

          console.log(new Date().toLocaleTimeString(‘chinese‘, { hour12: false }))//18:45:50 

          4、從Date()對(duì)象返回當(dāng)前 年份

          console.log(new Date().getFullYear())//2020

          5、從Date()對(duì)象返回當(dāng)前 月份

          注意:月份的返回值范圍是 0~11,所以要獲得當(dāng)前月份,要+1

          console.log(new Date().getMonth())//當(dāng)前月份-1
          console.log(new Date().getMonth()+1)//當(dāng)前月份

          6、從Date()對(duì)象返回月份的當(dāng)前 日

          console.log(new Date().getDate())//返回當(dāng)前 日

          7、從Date()對(duì)象返回一個(gè)星期的某一天,當(dāng)前是星期幾

          注意:獲取的返回值范圍是 0~6 , 0表示星期天

          console.log(new Date().getDay())

          8、從Date()對(duì)象的 當(dāng)前 小時(shí)

          注意:獲取返回值的范圍是 0~23

          console.log(new Date().getHours())//小時(shí)

          9、返回Date()對(duì)象的 當(dāng)前 分鐘

          注意:獲取返回值的范圍是 0~59

          console.log(new Date().getMinutes())//分

          10、返回Date()對(duì)象的 當(dāng)前 秒數(shù)

          注意:獲取返回值的范圍是 0~59

          console.log(new Date().getSeconds())//秒

          11、返回Date()對(duì)象的 當(dāng)前 毫秒數(shù)

          注意:范圍是 0~999

          console.log(new Date().getMilliseconds())//毫秒

          12、返回 日期 1970.01.01 距現(xiàn)在的毫秒數(shù)

          console.log(new Date().getTime())

          本文完~


          瀏覽 20
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  奴隷令嬢の屈辱调教鬼六 | 开心五月天激情网 | 色老板在线免费观看视频 | 美女操逼网站免费 | 日本色色图 |