js获取当前时间

发布时间 2023-08-24 11:07:12作者: jnmcok
 1 $("#current").html(currentDate(true, true))
 2 
 3 /*
 4 type:true(格式:2018年01月01日)
 5     false(格式:2018-01-01)
 6 isno: true(包含时分秒:2018-01-01 01:01:01)
 7     false(不包含时分秒:2018-01-01)
 8 */
 9 function currentDate(type, isno) {
10     var date = new Date();
11     var year = singular(date.getFullYear());
12     var month = singular(date.getMonth() + 1);
13     var strDate = singular(date.getDate());
14     var hours = singular(date.getHours());
15     var minutes = singular(date.getMinutes());
16     var seconds = singular(date.getSeconds());
17     if (type) {
18         if (isno) {
19             return year + "年" + month + "月" + strDate + "日 " + hours + "时" + minutes + "分" + seconds + "秒";
20         } else {
21             return year + "年" + month + "月" + strDate + "日";
22         }
23     } else {
24         if (isno) {
25             return year + "-" + month + "-" + strDate + " " + hours + ":" + minutes + ":" + seconds;
26         } else {
27             return year + "-" + month + "-" + strDate;
28         }
29     }
30 }
31 // 单数补位
32 function singular(num) {
33     if (num < 10) {
34         num = "0" + num;
35     }
36     return num;
37 }