JavaScript 获得当前日期+时间

//直接从项目中copy出来的,亲测可用。
function
getTodayTime(){ var date = new Date(); var seperator1 = "-";//可以自定义格式符,如/等 var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentDate = date.getFullYear() + seperator1 + month + seperator1 + strDate;//获得当前日期 var currentTime = date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();//获得当前时间 return currentDate+" "+currentTime;//此处返回日期+时间,如果不需要时间把时间去掉即可。 }

 原创文章,转载请注明出处,谢谢。

原文地址:https://www.cnblogs.com/pongyc/p/7569033.html