时间格式

时间格式,可以精确到秒,如2021-03-09 11:28:03 或 2021/03/09 11:28:03,个位月份或者天数等前面加上0

 1 function getFormatDate(date,idToSeconds,formatStr){
 2     //date 为new Date()形式的日期
 3     //idToSeconds 是否精确到秒
 4     //formatStr 为传递的格式形式,比如“-”或“/”,默认为“/”
 5     var formatStr = formatStr == undefined ? "/" : formatStr;
 6     var lastDate;
 7     var oMonth = date.getMonth() + 1;
 8     var oDate = date.getDate() + 1;
 9     var oHour = date.getHour() + 1;
10     var oMinutes = date.getMinutes() + 1;
11     var oSeconds = date.getSeconds() + 1;
12     if(oMonth<10) oMonth = "0" + oMonth;
13     if(oDate<10) oDate = "0" + oDate;
14     if(oHour<10) oHour = "0" + oHour;
15     if(oMinutes<10) oMinutes = "0" + oMinutes;
16     if(oSeconds<10) oSeconds = "0" + oSeconds;
17     if(isToSeconds){
18         lastDate = date.getFullYear() + formatStr + oMonth + formatStr + oDate + " " + oHour + ":" + oMinutes +":" + oSeconds;
19     }else{
20         lastDate = date.getFullYear() + formatStr + oMonth + formatStr + oDate;
21     }
22     return lastDate;
23 }

 

posted @ 2021-03-10 11:40  傅丹辰cds  阅读(1741)  评论(0编辑  收藏  举报