js获取当地时间并且拼接时间格式,在*上有人在问,查了资料,各种方法将时间格式改成任意自己想要的样式。
1.
- var date = new Date(+new Date()+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
- console.log(date);//2017-01-22 11:08:46
var date = new Date(+new Date()+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
console.log(date);//2017-01-22 11:08:46
2.
- var date = new Date();
- var strDate = date.toLocaleString().replace(/[年月]/g,'-').replace(/[日上下午]/g,'');
- console.log(strDate);// 2017/1/22 11:11:56
var date = new Date();
var strDate = date.toLocaleString().replace(/[年月]/g,'-').replace(/[日上下午]/g,'');
console.log(strDate);// 2017/1/22 11:11:56
3.字符串拼接方式
- function getNowFormatDate() {
- var date = new Date();
- var seperator1 = "-";
- var seperator2 = ":";
- var month = date.getMonth() + 1;
- var strDate = date.getDate();
- if (month >= 1 && month <= 9) {
- month = "0" + month;
- }
- if (strDate >= 0 && strDate <= 9) {
- strDate = "0" + strDate;
- }
- var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
- + " " + date.getHours() + seperator2 + date.getMinutes()
- + seperator2 + date.getSeconds();
- return currentdate;
- }
- var data = getNowFormatDate();
- console.log(data); //2017-01-22 11:14:16
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
} var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
var data = getNowFormatDate();
console.log(data); //2017-01-22 11:14:16</pre><br><br> </div>