您的位置:首页 > 其它

Coherence企业级缓存

2015-10-25 21:08 381 查看
一、概述
Date对象是JavaScript提供的日期和时间的操作接口。它有多种方法。

1.1、Date()
作为一个函数 Date() 可以直接调用,它返回当前时间和日期的字符串。
console.log(Date());   //Tue Mar 24 2015 19:06:23 GMT+0800 (中国标准时间)
console.log(Date(200,1,1))	//Tue Mar 24 2015 19:06:23 GMT+0800 (中国标准时间)
不管是否传入参数,直接调用只返回当前时间。

1.2、new Date()
Date 对象还是一个构造函数,对它使用new命令,会返回一个Date对象的实例,如果不加参数,生成的就是代表当前时间的对象。
var date = new Date();
console.log(date); //Tue Mar 24 2015 19:12:59 GMT+0800 (中国标准时间)
var date = new Date(2000,1,1);
console.log(date); //Tue Feb 01 2000 00:00:00 GMT+0800 (中国标准时间)
1.3、new Date( datestring )

Date对象还接受一个日期字符串作为参数,返回所对应的时间。

var date = new Date("2005-03-03");
console.log(date); //Thu Mar 03 2005 08:00:00 GMT+0800 (中国标准时间)
所有可以被Date.parse() 方法解析的日期字符串,都可以当做Date对象的参数

new Date("2013-02-15")
new Date("2013-FEB-15")
new Date("FEB, 15, 2013")
new Date("FEB 15, 2013")
new Date("Feberuary, 15, 2013")
new Date("Feberuary 15, 2013")
new Date("15, Feberuary, 2013")
// Fri Feb 15 2013 08:00:00 GMT+0800 (中国标准时间)
上述写法很多,返回的都是同一个时间。

1.4、new Date( year, month[,day,hours,minutes,seconds,ms] )
在多个参数的情况下,Date对象将其分别视作对应的年、月、日、小时、分钟、秒、毫秒。如果采用这个方法,最少需要指定两个参数(年和月),其他参数都可以不指定,默认为 0,如果只使用年一个参数,Date对象会将其解析为毫秒数。

new Date(2013) // Thu Jan 01 1970 08:00:02  GMT+0800 (中国标准时间)
new Date(2013,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,0,1) // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,0,1,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,0,1,0,0,0,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间)
上面除了第一行代码,返回的都是同一个时间,可以看到,月份是从0开始的,日是从1开始的。
如果参数超出了正常范围,会被自动折算。比如:月份为 15,就算为下一年的4月。参数还可以使用负数,表示扣去时间。

new Date(2013,0) // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,-1) // Sat Dec 01 2012 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,0,1) // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,0,0) // Mon Dec 31 2012 00:00:00 GMT+0800 (中国标准时间)
new Date(2013,0,-1) // Sun Dec 30 2012 00:00:00 GMT+0800 (中国标准时间)


年的情况有所不同,如果为 0,表示1900年;如果为负数,则表示公元前。

new Date(1,0) // Tue Jan 01 1901 00:00:00 GMT+0800 (中国标准时间)
new Date(0,0) // Mon Jan 01 1900 00:00:00 GMT+0800 (中国标准时间)
new Date(-1,0) // Fri Jan 01 -1 00:00:00 GMT+0800 (中国标准时间)


1.5、日期的运算

类型转换时,Date对象的实例如果转化为数值,则等于对应的毫秒数;如果转化为字符串,则等于对应的日期字符串,所以,两个日期对象进行减法运算,返回的就是他们之间的毫秒数;进行加法运算,返回的就是连接后的两个字符串。

var date1 = new Date(2000,1,1);
var date2 = new Date();
console.log(date2-date1);    //477862629379
console.log(date1+date2);
//Tue Feb 01 2000 00:00:00 GMT+0800 (中国标准时间)Tue Mar 24 2015 19:37:09 GMT+0800 (中国标准时间)


二、Date对象的方法
2.1、Date.now()
now方法返回当前距离1970年1月1日 00:00:00 UTC的毫秒数(Unix时间戳乘以1000)
console.log(Date.now()); //1427197175189
如果需要更精确的时间,可以使用window.performance.now()。它提供页面加载到命令运行时的已经过去的时间,单位是浮点数形式的毫秒。
console.log(window.performance.now());  //13.942999998107553


2.2、Date.parse()

parse方法是用来解析日期字符串,返回距离1970年1月1日00:00:00的毫秒数,日期字符串的格式应该完全或者部分符合RFC2822和ISO8061,即YYYY-MM-DDTHH:mm:ss:sssZ格式,其中最后的Z表示时区,是可选的。

Date.parse("Aug 9, 1995")// 返回807897600000,以下省略返回值
Date.parse("January 26, 2011 13:51:50")
Date.parse("Mon, 25 Dec 1995 13:30:00 GMT")
Date.parse("Mon, 25 Dec 1995 13:30:00 +0430")
Date.parse("2011-10-10")
Date.parse("2011-10-10T14:48:00")
如果解析失败,返回NaN。
Date.parse("xxx")  // NaN


三、Date实例对象的方法
使用new命令生成的Date对象的实例,有很多自己的方法。

3.1、Date.prototype.toString()

toString 方法返回一个完整的时间字符串。
var today = new Date();
console.log(today.toString());
//Tue Mar 24 2015 19:59:11 GMT+0800 (中国标准时间)
console.log(today);
//Tue Mar 24 2015 19:59:11 GMT+0800 (中国标准时间)
因为 today默认调用的是toString() 方法,所以两者是相同的。

3.2、Date.prototype.toISOString() Date.prototype.toUTCString()
toUTCString方法返回对应的UTC时间,也就是比北京时间晚8个小时。toISOString方法返回对应时间的ISO8601写法。
var today = new Date();
console.log(today);    //Tue Mar 24 2015 20:06:17 GMT+0800 (中国标准时间)
console.log(today.toUTCString());//Tue, 24 Mar 2015 12:06:17 GMT
console.log(today.toISOString());//2015-03-24T12:06:17.273Z


3.3、Date.prototype.toDateString() Date.prototype.toTimeString()
toDateString方法返回日期的字符串形式,toTimeString方法返回时间的字符串形式。
var today = new Date();
console.log(today.toDateString());//Tue Mar 24 2015
console.log(today.toTimeString());//20:13:03 GMT+0800 (中国标准时间)


3.4、Date.prototype.toLocaleDateString() Date.prototype.toLocaleTimeString()
toLocalDateString方法返回一个字符串,代表日期的当地写法;toLocalTimeString方法返回一个字符串,代表时间的当地写法。
var today = new Date();
console.log(today.toLocaleDateString());// Tue Mar 24 2015
console.log(today.toLocaleTimeString());//20:16:10 GMT+0800 (中国标准时间)


3.5、Date.prototype.valueOf()
valueOf方法返回实例对象距离1970年1月1日00:00:00 UTC对应的毫秒数,该方法等同于getTime方法。
var today = new Date();
console.log(today.valueOf());// 1427199446088
console.log(today.getTime());// 1427199446088
该方法可以用来计算精准的时间
var start = new Date();
//进行一些其他工作
var end = new Date();
var elapsed = end.getTime() - start.getTime();


3.6、Date.prototype.get系列方法
Date对象提供了一系列get方法,用来获取实例对象某个方面的值。
Date.prototype.getTime():返回实例对象距离1970年1月1日00:00:00对应的毫秒数,等同于valueOf方法。
Date.prototype.getDate():返回实例对象对应每个月的几号(从1开始)。
Date.prototype.getDay():返回星期,星期日为0,星期一为1,以此类推。
Date.prototype.getFullYear():返回四位的年份。
Date.prototype.getMonth():返回月份(0表示1月,11表示12月)。
Date.prototype.getHours():返回小时(0-23)。
Date.prototype.getMilliseconds():返回毫秒(0-999)。
Date.prototype.getMinutes():返回分钟(0-59)。
Date.prototype.getSeconds():返回秒(0-59)。

上面这些方法默认返回的都是当前时区的时间,Date对象还提供了这些方法对应的UTC版本,用来返回UTC时间,比如getUTCFullYear()、getUTCMonth()、getUTCDay()、getUTCHours()等等。


3.7、Date.prototype.set系列方法
Date对象提供了一系列set方法,用来设置实例对象的各个方面。
Date.prototype.setDate(date):设置实例对象对应的每个月的几号(1-31),返回改变后毫秒时间戳。
Date.prototype.setFullYear(year [, month, date]):设置四位年份。
Date.prototype.setHours(hour [, min, sec, ms]):设置小时(0-23)。
Date.prototype.setMilliseconds():设置毫秒(0-999)。
Date.prototype.setMinutes(min [, sec, ms]):设置分钟(0-59)。
Date.prototype.setMonth(month [, date]):设置月份(0-11)。
Date.prototype.setSeconds(sec [, ms]):设置秒(0-59)。
Date.prototype.setTime(milliseconds):设置毫秒时间戳。
set方法的参数都会自动折算。以setDate为例,如果参数超过当月的最大天数,则向下一个月顺延,如果参数是负数,表示从上个月的最后一天开始减去的天数。
var d = new Date("2013-02-06");
d.setDate(32)
// 1359648000000
console.log(d);
// Fri Feb 01 2013 00:00:00 GMT+0800 (CST)
var d = new Date ("2013-02-06");
d.setDate(-1)
// 1356796800000
console.log(d);
// Sun Dec 30 2012 00:00:00 GMT+0800 (CST)
set系列方法除了setTime(),都有对应的UTC版本,比如setUTCHours()。

参看链接:
http://www.w3school.com.cn/jsref/jsref_obj_date.asp

本文出自 “专注Java,linux技术” 博客,请务必保留此出处http://wuqinglong.blog.51cto.com/9087037/1623936
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: