JavaScript对时间(time)、日期(date)格式转换
将时间转化成(yyyy-MM-dd hh:mm:ss)格式
function dateTime() {
let date = new Date();
let seperator1 = "-";
let seperator2 = ":";
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
};
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
};
let hours = date.getHours();
if (hours >= 0 && hours <= 9) {
hours = "0" + hours;
};
let minutes = date.getMinutes();
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
};
let seconds = date.getSeconds();
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
};
let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + hours + seperator2 + minutes
+ seperator2 + seconds;
return currentdate;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 将时间转化成(yyyy年MM月dd日hh时mm分ss秒)格式
function get_date(){
let date = new Date(),
nowYear = date.getFullYear(),
nowMonth = date.getMonth() + 1, //注意getMonth从0开始,getDay()也是(此时0代表星期日)
nowDay = date.getDate(),
nowHour = date.getHours(),
nowMinute = date.getMinutes(),
nowSecond = date.getSeconds(),
weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
nowWeek = weekday[date.getDay()];
return nowYear + '年' + nowMonth + '月' + nowDay + '日' + nowHour + '时' + nowMinute + '分' + nowSecond + '秒' + nowWeek;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 将时间转化为标准时间
function get_unix_time(dateStr){
let newstr = dateStr.replace(/-/g,'/');
let date = new Date(newstr);
//let time_str = date.getTime().toString();
//return time_str.substr(0, 10);
return date;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 获取时间戳
function get_stamp(){
return new Date().getTime();
}
1
2
3
2
3
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13