JS 获取当前时间,格式为年月日时分秒

let getNowFormatDate = () => { //获取当前时间

  let date = new Date();

  let seperator1 = "."; //年月日之间的分隔

  let seperator2 = ":"; //时分秒之间的分隔

  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; //获取月,如果小于10,前面补个0

  let strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //获取日,如果小于10,前面补个0

  let strHours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //获取小时,如果小于10,前面补个0

  let strMinutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //获取分,如果小于10,前面补个0

  let strSeconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //获取秒,如果小于10,前面补个0

  let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +

​    " " + strHours + seperator2 + strMinutes +

​    seperator2 + strSeconds; //拼接一下

  return currentdate; //返回

}



let time = getNowFormatDate(); //打印输出:2020.04.02 12:34:56

版权声明:本文为hukuangjie原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hukuangjie/p/12619633.html