Date对象, 是操作日期和时间的对象。Date对象对日期和时间的操作只能通过方法。
参考:https://www.cnblogs.com/polk6/p/4156595.html
实例:
1、var date1 = new Date();
运行结果:
2、
toString() :将Date转换为一个'年月日 时分秒'字符串
toLocaleString() :将Date转换为一个'年月日 时分秒'的本地格式字符串
toDateString() :将Date转换为一个'年月日'字符串
toLocaleDateString() :将Date转换为一个'年月日'的本地格式字符串
toTimeString() :将Date转换为一个'时分秒'字符串
toLocaleTimeString() :将Date转换为一个'时分秒'的本地格式字符串
valueOf() :与getTime()一样, 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
运行结果:
get方法:
getFullYear() :返回Date对象的年份值;4位年份。
getMonth() :返回Date对象的月份值。从0开始,所以真实月份=返回值+1 。
getDate() :返回Date对象的月份中的日期值;值的范围1~31 。
getHours() :返回Date对象的小时值。
getMinutes() :返回Date对象的分钟值。
getSeconds() :返回Date对象的秒数值。
getMilliseconds() :返回Date对象的毫秒值。
getDay() :返回Date对象的一周中的星期值;0为星期天,1为星期一、2为星期二,依此类推
getTime() :返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00') 。
运行结果:
set方法
setFullYear(year, opt_month, opt_date) :设置Date对象的年份值;4位年份。
setMonth(month, opt_date) :设置Date对象的月份值。0表示1月,11表示12月。
setDate(date) :设置Date对象的月份中的日期值;值的范围1~31 。
setHours(hour, opt_min, opt_sec, opt_msec) :设置Date对象的小时值。
setMinutes(min, opt_sec, opt_msec) :设置Date对象的分钟值。
setSeconds(sec, opt_msec) :设置Date对象的秒数值。
setMilliseconds(msec) :设置Date对象的毫秒值。
运行结果:
Date.parse(dateStr)
说明:把字符串转换为Date对象 ,然后返回此Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
参数:
①dateStr {string} :可转换为Date对象的字符串(可省略时间);字符串的格式主要有两种:
1) yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的Date对象的时间为 00:00:00。
2) yyyy-MM-dd HH:mm:ss :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。若不省略时间,此字符串在IE中返回NaN(非数字)!
返回值:
{int} 返回转换后的Date对象与起始时间之间的毫秒数。
运行结果:
比较两个Date对象大小
工具
<script>
(function() {
/**
* 将时间改成自定义格式
*/
Date.prototype.Format = function (fmt) { //author: nxr
var o = {
'M+': this.getMonth() + 1, //月份
'd+': this.getDate(), //日
'h+': this.getHours(), //小时
'm+': this.getMinutes(), //分
's+': this.getSeconds(), //秒
'q+': Math.floor((this.getMonth() + 3) / 3), //季度
'S': this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
return fmt;
}
})(jQuery);
var date1 = new Date();
date1 = date1.Format('yyyy-MM-dd');
console.log(date1)
</script>
/*
* 功能:实现VBScript的DateAdd功能.
* 参数:interval,字符串表达式,表示要添加的时间间隔.
* 参数:number,数值表达式,表示要添加的时间间隔的个数.
* 参数:date,时间对象.
* 返回:新的时间对象.
* var now = new Date();
* var newDate = DateAdd('d', 5, now);
*--------------- DateAdd(interval,number,date) -----------------
*/
function dateAdd(interval, number, date) {
switch (interval) {
case 'y':
date.setFullYear(date.getFullYear() + number);
return date;
break;
case 'q':
date.setMonth(date.getMonth() + number * 3);
return date;
break;
case 'm':
date.setMonth(date.getMonth() + number);
return date;
break;
case 'w':
date.setDate(date.getDate() + number * 7);
return date;
break;
case 'd':
date.setDate(date.getDate() + number);
return date;
break;
case 'h':
date.setHours(date.getHours() + number);
return date;
break;
case 'mm':
date.setMinutes(date.getMinutes() + number);
return date;
break;
case 's':
date.setSeconds(date.getSeconds() + number);
return date;
break;
default:
date.setDate(date.getDate() + number);
return date;
break;
}
}