一、Date 构造函数
new Date();
new Date(milliseconds);
new Date(datestring);
new Date(year, month, day, hours, minutes, seconds, ms);
不带参数时,返回当前 CST 格式的日期和时间,例如:Thu Feb 01 2018 14:11:05 GMT+0800 (CST)。当传入一个数字参数时,这个参数是毫秒数,返回 getTime() 对应的那个日期。当传入一个字符串参数时,它将当做日期的字符串表示形式,格式为 Date.parse() 方法可以接受的格式。在其他情况下,应该向构造函数传入2~7个数字参数,用于指定日期及时间的各个字段。除了年和月,其余参数都是可选的。注意,这些日期和时间值是使用本地时间指定的,而不是世界时间(UTC)。
new Date() 返回的是当前电脑的系统时间。
注意:Safari浏览器对日期的格式要求比Chrome 更严格。Safari的 new Date() 只能处理像 2019/01/18 11:16 这种用斜杠 “/” 间隔的日期格式,而不能处理像 2019-01-18 11:16 这种短横线分隔的日期格式(会提示日期格式不合法),为了考虑兼容性,开发中尽量用斜杠分隔的日期格式。
二、获取日期
Date对象的大多数方法分为两种形式:一种使用本地时间;另一种使用世界时间(UTC或GMT)。如果一个方法的名字中有“UTC”,则它使用世界时间进行操作。一般支持本地时间的方法都支持世界时间,比如下面这些方法。
date.getFullYear(); //获取完整的年份(4位,1970)
date.getYear(); //获取年份(2位),已过时
date.getMonth(); //获取月份(0-11, 0代表1月,获取到的月份比中文的月份少1的,例如当前是12月份,获取的数字就是11,所以在显示当前时间的时候需要date.getMonth() + 1)
date.getDate(); //获取日(1-31)
date.getDay(); //获取星期?(0-6, 0代表星期天)
date.getHours(); //获取小时数(0-23)
date.getMinutes(); //获取分钟数(0-59)
date.getSeconds(); //获取秒数(0-59)
date.getMilliseconds(); //获取毫秒数(0-999)
date.getTime(); //返回从1970-1-1 00:00:00 UTC(世界标准时间)以来的毫秒数,对于1970-1-1 00:00:00 UTC之前的时间返回负值。(和 +date 得到的值一样)
date.toLocaleString(); 返回本地格式的日期与时间(例如:2020/9/26 上午9:29:55)
date.toLocaleDateString(): 返回本地格式的日期(例如:2020/9/26)
date.toLocaleTimeString(): 返回本地格式的时间(例如:上午9:29:55)
Date.now(); // 跟 date.getTime() 效果一致
Date.parse('2020/11/15 10:40:00'); // 解析一个字符串表示的日期为毫秒数
三、设置日期
设置日期的方法的参数都是 Number 类型,而且他们都会返回调整过后的时间的毫秒数。
date.setTime() // 使用毫秒的格式,设置一个Date对象的值
date.setFullYear(year, month, day) // 设置年份(以及可选的月份及日期)date.setFullYear(2020)
date.setMonth(month, day) // 设置月份0(1月)~11(12月) (以及可选的日期)
date.setDate() // 设置日期
date.setHours(hours, minutes, seconds, millis) // 设置小时数 (以及可选的分钟、秒及毫秒)
date.setMinutes(minutes, seconds, millis) // 设置分钟数 (以及可选的秒及毫秒)
date.setSeconds(seconds, millis) // 设置秒数 (以及可选的毫秒)
date.setMilliseconds() // 设置毫秒数
四、日期常用示例
1、将2005-8-5转换成2005-08-05格式
var strDate = '2005-8-5';
window.alert(strDate.replace(/\b(\w)\b/g, '0$1'));
2、得到间隔天数
alert("间隔天数为:"+(new Date('2005/8/15')-new Date('2003/9/18'))/1000/60/60/24+"天") ;
3、得到今天的日期
d = new Date();
alert(d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日");
4、得到前N天或后N天的日期
方法一:
function showdate(n)
{
var uom = new Date(new Date()-0+n*86400000); // -0是为了转换为数字类型,new Date()直接 “+” 会变成字符串拼接。
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
方法二:
function showdate(n)
{
var uom = new Date();
uom.setDate(uom.getDate()+n);
uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
return uom;
}
window.alert("今天是:"+showdate(0));
window.alert("昨天是:"+showdate(-1));
window.alert("明天是:"+showdate(1));
window.alert("10天前是:"+showdate(-10));
window.alert("5天后是:"+showdate(5));
5. 日期月日时分秒,10:8:9 格式化为 10:08:09 的方法?
formatNumber (n) {
n = n.toString();
return n[1] ? n : '0' + n;
}