JSは現在の日付とタイムスタンプと関連操作を取得します.

1981 ワード

現在の時間を取得します.正確なパラメータがあります. 



    
    current



  /**
   *      
   *       
   *       
   */
  getCurrent = (arg) => {
    const current = new Date();
    const year = current.getFullYear();   //     
    let month = current.getMonth();       //     
    let date = current.getDate();         //     
    let day = current.getDay();           //     
    let hour = current.getHours();        //     
    let minute = current.getMinutes();    //     
    let sec = current.getSeconds();       //    
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minute < 10) minute = "0" + minute;
    if (sec < 10) sec = "0" + sec;
    let time = "";

    if(arg){ //    
      time = year + "-" + month + "-" + date;
    } else { //    
      time = year + "-" + month + "-" + date+ " " + hour + ":" + minute + ":" + sec;
    }
    return time;
  };
  console.log(getCurrent(1));

  //              
  let current = (new Date()).toLocaleDateString();
  current =current.replace(/\//g,'-');
  console.log(curent);



現在のタイムスタンプを取得:
var timestamp = Date.parse(new Date());
console.log(timestamp);
//1546352356000
//         ,     0  ;

var timestamp = (new Date()).valueOf();
console.log(timestamp);
//1546352384929

var timestamp = new Date().getTime();
console.log(timestamp);
//1546352498682
両者の間の相互変換:
//       
const timestamp= (new Date("2019/01/01 22:30:29"))/1000;
//   
const timestamp= (new Date("2019-01-01 22:30:29"))/1000;

//             
const timestamp= (new Date("2019/01/01"))/1000;
//   
const timestamp= (new Date("2019-01-01"))/1000;

//       
getTime = (ts) => { //       13       1000
    return new Date(parseInt(ts) * 1000).toLocaleString()      
}