js获取当前时间并转化
程序员文章站
2022-05-18 22:44:23
1.转化为 年月日 2.转化为 年月日 时分秒 ......
1.转化为 年月日
function getnowformatdate() { var date = new date(); var seperator1 = "-"; var year = date.getfullyear(); 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 = year + seperator1 + month + seperator1 + strdate; return currentdate; }
2.转化为 年月日 时分秒
function getnowtime() { var date = new date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getmonth() + 1<10? "0"+(date.getmonth() + 1):date.getmonth() + 1; var strdate = date.getdate()<10? "0" + date.getdate():date.getdate(); var currentdate = date.getfullyear() + seperator1 + month + seperator1 + strdate + " " + date.gethours() + seperator2 + date.getminutes() + seperator2 + date.getseconds(); return currentdate; }