当前位置: 首页 > Web前端 > HTML

js时间日期格式

时间:2023-03-28 16:09:27 HTML

日期格式化方法exportfunctiondateFormat(date,format){format=format||'yyyy-MM-ddhh:mm:ss';if(date!=='InvalidDate'){leto={"M+":date.getMonth()+1,//month"d+":date.getDate(),//day"h+":date.getHours(),//小时"m+":date.getMinutes(),//分钟"s+":date.getSeconds(),//秒"q+":Math.floor((date.getMonth()+3)/3),//quarter"S":date.getMilliseconds()//毫秒}if(/(y+)/.test(format))format=format.replace(RegExp.$1,(date.getFullYear()+"").substr(4-RegExp.$1.length));for(letkino)if(newRegExp("("+k+")").test(format))format=format.replace(RegExp.$1,RegExp.$1.length===1?o[k]:("00"+o[k]).substr((""+o[k]).length));返回格式;}return'';}使用方法:dateFormat(newDate(),'yyyy年MM月dd日')//2023年3月2日dateFormat(newDate(),'yyyy-MM-ddhh:mm:ss')//2023-03-0211:22:30dateFormat(newDate(),'yyyy-MM-dd')//2023-03-02

最新推荐
猜你喜欢