js获取当前日期并且格式化(JS get the current date and format it)-js
js获取当前日期并且格式化(JS get the current date and format it)
简介经常会用的到,通过js获取系统当前时间戳,并且对时间戳格式化
直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
|
function get_now_date(){ var timestamp = Date.parse( new Date()); var date = new Date(timestamp); //获取年份 var Y =date.getFullYear(); //获取月份 var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); //获取当日日期 var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); return Y+ '-' +M+ '-' +D; } |
function
get_now_date(){
var
timestamp = Date.parse(
new
Date());
var
date =
new
Date(timestamp);
//获取年份
var
Y =date.getFullYear();
//获取月份
var
M = (date.getMonth() + 1 < 10 ?
'0'
+ (date.getMonth() + 1) : date.getMonth() + 1);
//获取当日日期
var
D = date.getDate() < 10 ?
'0'
+ date.getDate() : date.getDate();
return
Y+
'-'
+M+
'-'
+D;
}
————————
< strong > Introduction < / strong > it is often used to obtain the current time stamp of the system through JS and format the time stamp
Direct code:
1
2
3
4
5
6
7
8
9
10
11
12
|
function get_now_date(){ var timestamp = Date.parse( new Date()); var date = new Date(timestamp); //获取年份 var Y =date.getFullYear(); //获取月份 var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); //获取当日日期 var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); return Y+ '-' +M+ '-' +D; } |
function
get_now_date(){
var
timestamp = Date.parse(
new
Date());
var
date =
new
Date(timestamp);
//获取年份
var
Y =date.getFullYear();
//获取月份
var
M = (date.getMonth() + 1 < 10 ?
'0'
+ (date.getMonth() + 1) : date.getMonth() + 1);
//获取当日日期
var
D = date.getDate() < 10 ?
'0'
+ date.getDate() : date.getDate();
return
Y+
'-'
+M+
'-'
+D;
}