目录
一、常用date方法
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-???)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
二、Date函数
1.获取当前年月日
function getYearDate() {
let tempDate = null;
let d = new Date;var year = d.getFullYear();
let month = (d.getMonth() + 1) >=10? (d.getMonth() + 1) : "0" + (d.getMonth() + 1);
let date = d.getDate() >=10 ? d.getDate() : "0" + d.getDate();
return tempDate = year + "-" + month + "-" + date;
}
console.log(getYearDate()); //"2020-09-07"
2.获取当前时间
function getNowTime(){
let date=new Date();
return date.getFullYear()+"-"
+(date.getMonth()+1).toString().padStart(2,"0")+"-"
+date.getDate().toString().padStart(2,"0")
+" "+date.getHours().toString().padStart(2,"0")
+":"+date.getMinutes().toString().padStart(2,"0")
+":"+date.getSeconds().toString().padStart(2,"0");
}
console.log(getNowTime()); //"2020-09-07 14:19:28"
3.获取给定时间戳的时间
function formatDateTime(timestamp) {
let date = new Date(timestamp);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
console.log(formatDateTime(1599459858000)); //"2020-09-07 14:24:18"
4.获取当前时间前后的时间
function formatDateTime(timestamp) {
let date = new Date(timestamp);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
console.log(formatDateTime(1599459858000)); //"2020-09-07 14:24:18"
5.获取当前年份前后的年份
function formatDateTime(timestamp) {
let date = new Date(timestamp);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
console.log(formatDateTime(1599459858000)); //"2020-09-07 14:24:18"
6.获取当前时间的前一个小时时间
function formatDateTime(timestamp) {
let date = new Date(timestamp);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
console.log(formatDateTime(1599459858000)); //"2020-09-07 14:24:18"
7.将时间分隔符去除
function formatDateTime(timestamp) {
let date = new Date(timestamp);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
console.log(formatDateTime(1599459858000)); //"2020-09-07 14:24:18"
8.开始日期到截至日期的天数
function formatDateTime(timestamp) {
let date = new Date(timestamp);
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};
console.log(formatDateTime(1599459858000)); //"2020-09-07 14:24:18"
三、方法注意点
1.getYear和getFullYear两种方法返回区别
首先,无论何时都应使用getFullYear来替代getYear方法。
var d=new Date;
alert(d.getYear())
// =>IE中返回 2021,在Firefox中会返回121
// (2021-1900=121)
alert(d.getFullYear())
// =>IE和FF中都会返回2021
2.getMonth获取月份需要加1
var d=new Date;
alert(d.getMonth())
// =>返回值0-11