<script type="text/javascript">
/**
* 小生爱 - 2015.01-19
* Date - 日期
** 时间基准 : 以1970.1.1 08:00:00为0,0点 所有值都从这个时间进行加减
** 创建日期对象的方式
* new Date() : 没有任何参数 返回当前日期
* new Date(数值) : 以时间戳的方式创建日期对象 如果参数为一个数字 将视该数字为毫秒值 转换为日期
* new Date('年-月-日 小时:分:秒:毫秒')
new Date('年.月.日 小时:分:秒:毫秒') :
以字符的方式创建日期对象 年份的最小值100(100以下的值会自 动补值 有时候会报错 所以不建议写100
以下的值) 字符串创建日期方式 可以省略其中的某些值 很方便
* new Date(年, 月)
new Date(年, 月, 日)
new Date(年, 月, 日, 小时)
new Date(年, 月, 日, 小时, 分)
new Date(年, 月, 日, 小时, 分, 秒)
new Date(年, 月, 日, 小时, 分, 秒, 毫秒) :
以数字的方式创建日期 需要注意代表月的数字范围 0 - 11 0 == 1月,1 == 2月。。。与字符串不同
** 方法
** GMT get方法
* getDate() : 返回日期中的天数 1-31
* getDay() : 返回星期 0-6 0==周日 1-6==周一-周六
* getFullYear() : 返回年数
* getHours() : 返回小时0-23
* getMilliseconds() : 返回当前毫秒数 过期的时间毫秒数永远为0
* getMinutes() : 返回分钟 0-59
* getMonth() : 返回月份 0-11
* getSeconds() : 返回秒
* getTime() : 到1970.1.1 18:00:00 的毫秒数
** UTC时间方法 (*注:比GMT时间晚了8个小时 其他基本没有区别)
* getTimezoneOffset() : 返回本地时间与用UTC表示当前日期的时间差 以分钟为单位
日期对象.getTimezoneOffset() 日期对象随便哪个都可以
new Date().getTimezoneOffset() == new Date(随便数字).getTimezoneOffset()
* getUTCDate() : 返回UTC日期
* getUTCDay() : 返回UTC星期
* getUTCFullYear() : 返回UTC年
* getUTCHours() : 返回UTC小时
* getUTCMilliseconds() : 返回UTC毫秒数
* getUTCMinutes() : 返回UTC分钟
* getUTCMonth() : 返回UTC月份
* Date.UTC(year, month)
Date.UTC(year, month, day)
Date.UTC(year, month, day, hours)
Date.UTC(year, month, day, hours, getMinutes)
Date.UTC(year, month, day, hours, getMinutes, getSeconds)
Date.UTC(year, month, day, hours, getMinutes, getSeconds, microseconds) :
返回设置的时间与世界标准时间1970.1.1 00:00:00之间的时间戳
* toUTCString() : 返回UTC时间字符串 new Date().toUTCString()
** GMT set方法
* setDate(1-31) : 设置日期 返回时间戳
* setMonth(month, day) : 设置月(日 可选) 返回时间戳
* setFullYear(year, month, day) : 设置年(月 日 可选) 返回时间戳
* setHours(hours, minutes, seconds, microseconds) : 设置小时(分 秒 毫秒可选) 返回时间戳
* setMinutes(minutes, seconds, microseconds) : 设置分钟(秒 毫秒可选) 返回时间戳
* setSeconds(seconds, microseconds) : 设置秒(毫秒 可选) 返回时间戳
* setMilliseconds(microseconds) : 设置毫秒 返回时间戳
* setTime(时间戳) : 设置date对象的时间戳
** UTC set方法 : 设置当前日期对象
* setUTCDate(1~31) : 设置UTC日 返回时间戳
* setUTCMonth([0~11, day]) : 设置UTC月 日 返回时间戳
* setUTCFullYear([year, month, day]) : 设置UTC年 月 日 返回时间戳
* setUTCHours([hours, minutes, seconds, microseconds]) : 设置UTC小时 分 秒 毫秒 返回时间戳
* setUTCMinutes([minutes, seconds, microseconds]) : 设置UTC分 秒 毫秒 返回时间戳
* setUTCSeconds([seconds, microseconds]) : 设置UTC秒 毫秒 返回时间戳
* setUTCMilliseconds(microseconds) : 设置UTC毫秒 返回时间戳
** 其他方法
* toTimeString() : 把Date的时间部分转化为字符串 并返回
* toDateString() : 把Date的日期部分转化为字符串 并返回
* toLocaleString() : 把日期转换为数字的字符串格式
* toLocaleTimeString() : 把时间转换为数字的字符串格式
* toLocaleDateString() : 把日期转换为数字的字符串格式
*/
var d = new Date('196-2-1 18:11:22');
// Mon Feb 01 196 18:11:22 GMT+0800 (中国标准时间)
console.log(d);
// 返回天数 1-31 输出1号
console.log('号 : ' + d.getDate());
// 返回星期0-6 0代表周日 其他正常 输出周一
console.log('星期 : ' + d.getDay());
// 返回年份 196
console.log('年份 : ' + d.getFullYear());
// 返回小时
console.log('小时 : ' + d.getHours());
// 返回当前毫秒数 0-999
// 对于已经过去的时间 毫秒数永远为0
console.log('毫秒数 : ' + d.getMilliseconds());
// 获取分钟
console.log('分 : ' + d.getMinutes());
// 获取月
console.log('月 : ' + d.getMonth());
// 秒
console.log('秒 : ' + d.getSeconds());
// 返回本地时间与用UTC表示当前日期的时间差 以分钟为单位
// 输出-480分钟
console.log('UTC与GMT时间差 : ' + d.getTimezoneOffset() );
// UTC日期
console.log('UTC日期 : ' + d.getUTCDate());
// UTC星期
console.log('UTC星期 : ' + d.getUTCDay());
// UTC年份
console.log('UTC年 : ' + d.getUTCFullYear());
// UTC小时
console.log('UTC小时 : ' + d.getUTCHours());
// UTC当前毫秒数
console.log('UTC毫秒数 : ' + d.getUTCMilliseconds());
// UTC分钟
console.log('UTC分钟 : ' + d.getUTCMinutes());
// UTC月
console.log('UTC月 : ' + d.getUTCMonth());
// UTC秒数
console.log('UTC秒数 : ' + d.getUTCSeconds());
// UTC距1970.1.1 00:00:00的毫秒数
console.log('UTC' + Date.UTC(1980, 3, 1));
// toUTCString() : 返回date对象的世界标准时间(UTC)的字符串表示
// Tue, 01 Apr 1980 00:00:00 GMT
console.log('UTC字符串 : ' + new Date( Date.UTC(1980, 3)).toUTCString() );
// 返回GMT的字符串表示 建议使用字符串形式创建日期 可以省略参数 更灵活
// Tue Apr 01 1980 00:00:00 GMT+0800 (中国标准时间)
console.log('字符串形式创建日期 : ' + new Date('1980-4'));
// 把UTC时间戳转换成 GMT字符串表示
// Tue Apr 01 1980 08:00:00 GMT+0800 (中国标准时间)
console.log('UTC时间戳转化成GMT时间 : ' + new Date( Date.UTC(1980, 3) ));
// set ______________________________________________________________________
// 设置日 返回时间戳
console.log('setDate : ' + new Date().setDate(22));
// 设置月 返回时间戳
console.log('setMonth : ' + new Date().setMonth(4, 21));
// 设置年 返回时间戳
console.log('setFullYear : ' + new Date().setFullYear(198, 3, 2));
// 设置小时 返回时间戳
console.log('setHours : ' + new Date().setHours(15, 16, 15, 17));
// 设置分钟 返回时间戳
console.log('setMinutes : ' + new Date().setMinutes(55, 10, 10));
// 设置秒 返回时间戳
console.log('setSeconds : ' + new Date().setSeconds(20, 10));
// 设置毫秒 返回时间戳
console.log('setMilliseconds : ' + new Date().setMilliseconds(99));
// 以毫秒设置date对象
console.log('setTime : ' + new Date().setTime(1422012022099));
// 设置UTC日期 以1970.1.1 00:00:00 返回时间戳
console.log('setUTCDate : ' + new Date().setUTCDate(15));
// 设置月 返回时间戳
console.log('setUTCMonth : ' + new Date().setUTCMonth(1, 29));
// 设置年 返回时间戳
console.log( 'setUTCFullYear : ' + new Date().setUTCFullYear(2001, 1, 29) );
// 设置小时 返回时间戳
console.log('setHours : ' + new Date().setUTCHours(14, 58, 55, 500));
// 设置分钟 返回时间戳
console.log('setUTCMinutes : ' + new Date().setUTCMinutes(59, 55, 500));
// 设置秒 返回时间戳
console.log('setSeconds : ' + new Date().setUTCSeconds(50, 500));
// 设置毫秒 返回时间戳
console.log('setUTCMilliseconds : ' + new Date().setUTCMilliseconds(500));
// toTimeString : 把Date对象的时间部分转换成字符串 并返回
console.log('时间部分的字符串 : ' + new Date().toTimeString());
// toDateString : 把Date对象的日期部分转换成字符串 并返回
console.log('日期部分的字符串 : ' + new Date().toDateString());
// 把日期转换为数字的字符串格式 1900/4/24 上午12:05:43
console.log( '数字的字符串格式 : ' + new Date(1900, 3, 24).toLocaleString() );
// 把时间转换为数字的字符串格式
console.log('时间的数字字符串格式 : ' + new Date().toLocaleTimeString());
// 把日期转换为数字的字符串格式
console.log('日期的数字字符串格式 : ' + new Date().toLocaleDateString());
console.log( new Date(2000000).toLocaleTimeString() );
</script>