javaScriptのData関数

19025 ワード

1、現在時間の取得
Date()が取得した時間は現在の機器の表示時間です.
let nowTime = new Date(); //       
data時間を通常のフォーマットに変換します.
<span style="color: #000000;">
            let getTimeNow </span>= () =><span style="color: #000000;"> {
                let nowTime </span>= <span style="color: #0000ff;">new</span> Date(); <span style="color: #008000;">//</span><span style="color: #008000;">       ——Sun Jan 12 2020 14:07:22 GMT+0800 (      )</span>
                let year = nowTime.getFullYear(); <span style="color: #008000;">//</span><span style="color: #008000;">  </span>
                let month = nowTime.getMonth()+1; <span style="color: #008000;">//</span><span style="color: #008000;">       1</span>
                let day = nowTime.getDate(); <span style="color: #008000;">//</span><span style="color: #008000;">     </span>
                let hour = nowTime.getHours();<span style="color: #008000;">//</span><span style="color: #008000;">    </span>
                let minu = nowTime.getMinutes();<span style="color: #008000;">//</span><span style="color: #008000;">    </span>
                let sec = nowTime.getSeconds();<span style="color: #008000;">//</span><span style="color: #008000;">   </span>
                let week = nowTime.getDay();<span style="color: #008000;">//</span><span style="color: #008000;">     </span>
                let timeStr = ''<span style="color: #000000;">;
                timeStr </span>+= year + '/' + isGreaterthanTen(month) + '/' +<span style="color: #000000;"> isGreaterthanTen(day); 
                timeStr </span>+= '-' + isGreaterthanTen(hour) + '/' + isGreaterthanTen(minu) + '/' +<span style="color: #000000;"> isGreaterthanTen(sec);
                timeStr </span>+= '-' +<span style="color: #000000;"> getWeek(week);
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> timeStr;
            };
            let isGreaterthanTen </span>= num =><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">if</span> (parseInt(num, 10) >= 10<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> num;
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    </span><span style="color: #0000ff;">return</span> '0' +<span style="color: #000000;"> num;
                }
            };
            let getWeek </span>= week =><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">if</span> (parseInt(week, 10) === 0<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 1<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 2<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 3<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 4<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 5<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                }
            };
            console.log(getTimeNow()); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 2020/01/12-14/25/29-   </span>
        
 
2、タイムスタンプ
 現在のタイムスタンプを取得:
       //        
            let timestamp1 = Date.parse(new Date()); //     
            let timestamp2 = (new Date()).valueOf(); //      
            let timestamp3 = new Date().getTime(); //      
            let timestamp4 = Number(new Date()) ; //      
            let timestamp5 = Date.now(); //         
 
 
 
時間をタイムスタンプに変換する
<span style="color: #000000;">
            let getTimeNow </span>= (nowTime) =><span style="color: #000000;"> {
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> let nowTime = new Date(); //       ——Sun Jan 12 2020 14:07:22 GMT+0800 (      )</span>
                let year = nowTime.getFullYear(); <span style="color: #008000;">//</span><span style="color: #008000;">  </span>
                let month = nowTime.getMonth()+1; <span style="color: #008000;">//</span><span style="color: #008000;">       1</span>
                let day = nowTime.getDate(); <span style="color: #008000;">//</span><span style="color: #008000;">     </span>
                let hour = nowTime.getHours();<span style="color: #008000;">//</span><span style="color: #008000;">    </span>
                let minu = nowTime.getMinutes();<span style="color: #008000;">//</span><span style="color: #008000;">    </span>
                let sec = nowTime.getSeconds();<span style="color: #008000;">//</span><span style="color: #008000;">   </span>
                let week = nowTime.getDay();<span style="color: #008000;">//</span><span style="color: #008000;">     </span>
                let timeStr = ''<span style="color: #000000;">;
                timeStr </span>+= year + '/' + isGreaterthanTen(month) + '/' +<span style="color: #000000;"> isGreaterthanTen(day); 
                timeStr </span>+= '-' + isGreaterthanTen(hour) + '/' + isGreaterthanTen(minu) + '/' +<span style="color: #000000;"> isGreaterthanTen(sec);
                timeStr </span>+= '-' +<span style="color: #000000;"> getWeek(week);
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> timeStr;
            };
            let isGreaterthanTen </span>= num =><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">if</span> (parseInt(num, 10) >= 10<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> num;
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    </span><span style="color: #0000ff;">return</span> '0' +<span style="color: #000000;"> num;
                }
            };
            let getWeek </span>= week =><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">if</span> (parseInt(week, 10) === 0<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 1<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 2<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 3<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 4<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (parseInt(week, 10) === 5<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    </span><span style="color: #0000ff;">return</span> '   '<span style="color: #000000;">
                }
            };
            </span><span style="color: #008000;">//</span><span style="color: #008000;">        </span>
            let timestamp1 = Date.parse(<span style="color: #0000ff;">new</span> Date()); <span style="color: #008000;">//</span><span style="color: #008000;">     </span>
            let timestamp2 = (<span style="color: #0000ff;">new</span> Date()).valueOf(); <span style="color: #008000;">//</span><span style="color: #008000;">      </span>
            let timestamp3 = <span style="color: #0000ff;">new</span> Date().getTime(); <span style="color: #008000;">//</span><span style="color: #008000;">      </span>
            let timestamp4 = Number(<span style="color: #0000ff;">new</span> Date()) ; <span style="color: #008000;">//</span><span style="color: #008000;">      </span>
            let timestamp5 = Date.now(); <span style="color: #008000;">//</span><span style="color: #008000;">         </span>
<span style="color: #000000;">            
            let timeStr </span>= <span style="color: #0000ff;">new</span> Date(1578811134211); <span style="color: #008000;">//</span><span style="color: #008000;">      </span>
            console.log(getTimeNow(timeStr)); <span style="color: #008000;">//</span><span style="color: #008000;"> 2020/01/12-14/38/54-   </span>
            console.log(timeStr.toLocaleDateString().replace(/\//g, "-") + " " + timeStr.toTimeString().substr(0, 8)); <span style="color: #008000;">//</span><span style="color: #008000;">             yyyy-MM-dd hh:mm:ss   </span>
        
3、カウントダウン
タイムスタンプを計算します.
<span style="color: #000000;">            
            let timeEnd </span>= 1578987896323; <span style="color: #008000;">//</span><span style="color: #008000;">          </span>
<span style="color: #000000;">            
            let countDown </span>= (timeEnd) =><span style="color: #000000;"> {
                let timeNow </span>= <span style="color: #0000ff;">new</span> Date().getTime(); <span style="color: #008000;">//</span><span style="color: #008000;">      </span>
                let remainingTime = parseInt(timeEnd) -<span style="color: #000000;"> parseInt(timeNow);
                secondTime(remainingTime);
            };
            let isGreaterthanTen </span>= num =><span style="color: #000000;"> {
                </span><span style="color: #0000ff;">if</span> (parseInt(num, 10) >= 10<span style="color: #000000;">) {
                    </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> num;
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    </span><span style="color: #0000ff;">return</span> '0' +<span style="color: #000000;"> num;
                }
            };
            
            let secondTime </span>= (remainingTime) =><span style="color: #000000;"> {
                </span><span style="color: #008000;">/*</span><span style="color: #008000;">
                 1   s = 1000    ms
                 1   min = 60   s
                 1   hour = 60  min
                 1   day = 24    hour
                 1    86400000  (ms)
                 * </span><span style="color: #008000;">*/</span><span style="color: #000000;">
                let timeStr </span>= ''<span style="color: #000000;">;
                let days </span>= Math.floor(remainingTime/(24*3600*1000)); //<span style="color: #000000;">     
                </span><span style="color: #0000ff;">if</span> (days > 0<span style="color: #000000;">) {
                    timeStr </span>+= isGreaterthanTen(days) + ' '<span style="color: #000000;">;
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    timeStr </span>+= '00 '<span style="color: #000000;">
                }
                
                let hoursLeave </span>= remainingTime%(24*3600*1000)    <span style="color: #008000;">//</span><span style="color: #008000;">           </span>
                let hours = Math.floor(hoursLeave/(3600*1000));  //<span style="color: #000000;">      
                </span><span style="color: #0000ff;">if</span> (hours > 0<span style="color: #000000;">) {
                    timeStr </span>+= isGreaterthanTen(hours) + ' '<span style="color: #000000;">;
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    timeStr </span>+= '00 '<span style="color: #000000;">;
                } 
                
                let minLeave </span>= hoursLeave%(3600*1000);        <span style="color: #008000;">//</span><span style="color: #008000;">            </span>
                let minutes = Math.floor(minLeave/(60*1000));
                <span style="color: #0000ff;">if</span> (minutes > 0<span style="color: #000000;">) {
                    timeStr </span>+= isGreaterthanTen(minutes) + ' '<span style="color: #000000;">;
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    timeStr </span>+= '00 '<span style="color: #000000;">;
                }
                
                let secounLeave </span>=minLeave%(60*1000);      <span style="color: #008000;">//</span><span style="color: #008000;">            </span>
                let seconds=Math.round(secounLeave/1000);
                <span style="color: #0000ff;">if</span> (seconds > 0<span style="color: #000000;">) {
                    timeStr </span>+= isGreaterthanTen(seconds) + ' '<span style="color: #000000;">
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    timeStr </span>+= '00 '<span style="color: #000000;">;
                }
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> timeStr;
            }
            console.log(countDown(timeEnd));
        </span>
4、yyy-M-dd HH:mm:ssをタイムスタンプに変換する
            //             
            let stringTime = "2020-01-01 00:00:00";
            let timestamp = (new Date(stringTime)).getTime();
            console.log(timestamp);