码迷,mamicode.com
首页 > 其他好文 > 详细

Date

时间:2017-07-07 14:18:11      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:日期   自己   相同   16px   gets   字符   valueof   时间   一周   

整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,

使用一个统一的时间,称为通用协调时UTC(UTC, Universal Time Coordinated),

UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同北京时区是东八区,领先UTC八个小时

 

 

 

一、new Date()                      //当前的本地日期和时间

 

        var dt = new Date();

        console.log(dt)

 

二、new Date(ms)               //把毫秒数转换为Date对象,表示从‘1970/01/01 00:00:00‘为起点,开始叠加的毫秒数,

                                          注意:起点的时分秒还要加上当前所在的时区,北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00

 

 

var dt = new Date(1000 * 60 * 1);           // 前进1分钟的毫秒数

console.log(dt); // 1970/01/01 08:01:00

dt = new Date(-1000 * 60 * 1);             // 倒退1分钟的毫秒数

console.log(dt); //1970/01/01 07:59:00

 

 

 

三、new Date(dateStr)               //把字符串转换为Date对象

换为Date对象的字符串(可省略时间)的格式主要有两种:

yyyy/MM/dd HH:mm:ss(都用的这种)若省略时间,返回的Date对象的时间为 00:00:00

yyyy-MM-dd HH:mm:ss若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区),若不省略时间,此字符串在IE中会转换失败

 

var dt = new Date(‘2017/12/25‘);                 // yyyy/MM/dd

console.log(dt);                                            // 2017/12/25 00:00:00

dt = new Date(‘2017/12/25 12:00:00‘);        // yyyy/MM/dd HH:mm:ss

console.log(dt); //2017/12/25 12:00:00

 

dt = new Date(‘2017-12-25‘);                         // yyyy-MM-dd

console.log(dt);                                               //2017-12-25 08:00:00 (加上了东8区的时区)

dt = new Date(‘2017-12-25 12:00:00‘);           // yyyy-MM-dd HH:mm:ss (注意:此转换方式在IE中会报错!)

console.log(dt); // 2017-12-25 12:00:00

 

 

四、new Date(year, month, opt_day, opt_hours, opt_minutes, opt_seconds, opt_milliseconds)

把年月日、时分秒转换为Date对象        opt_==>option,意思是可以不填,

参数:

year:年份4位数字,如:1999、2014

month:月份2位数字,从0开始计算,0表示1月份、11表示12月份。

opt_day可选号2位数字,从1开始计算,1表示1号。

opt_hours可选时2位数字,取值0~23。                (取的值可以超出范围,系统自动累加)

opt_minutes可选分2位数字,取值0~59。             (取的值可以超出范围,系统自动累加)

opt_seconds可选秒2位数字,取值0~59。             (取的值可以超出范围,系统自动累加)

opt_milliseconds可选毫秒,取值0~999。               (取的值可以超出范围,系统自动累加)

 

var dt = new Date(2017, 1); // 2017年2月(这里输入的月份数字为1)

console.log(dt); // 2017/2/01 00:00:00

dt = new Date(2017, 1, 25); // 2017年2月25日

console.log(dt); //2017/2/25 00:00:00

dt = new Date(2017, 1, 25, 15, 30, 40); // 2017年2月25日 15点30分40秒

console.log(dt); // 2017/2/25 15:30:40

dt = new Date(2017, 12, 25); // 2017年13月25日(这里输入的月份数字为12,表示第13个月,跳转到第二年的1月)

console.log(dt); // 2018/01/25

 

 

五、实例方法

 Date对象的方法分为2种形式:  本地时间    和    UTC时间

 同一个方法,一般都会有2种时间格式操作(方法名带UTC的,就是操作UTC时间)。

 

1、get方法

getFullYear() 返回Date对象的年份值;4位年份。

getMonth() 返回Date对象的月份值。从0开始,所以真实月份=返回值+1 。

getDate() 返回Date对象的月份中的日期值;值的范围1~31 。

getHours() 返回Date对象的小时值。

getMinutes() 返回Date对象的分钟值。

getSeconds() 返回Date对象的秒数值。

getMilliseconds()返回Date对象的毫秒值。

getDay() 返回Date对象的一周中的星期值;0为星期天,1为星期一

getTime() 返回Date对象与‘1970/01/01 00:00:00‘之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00‘) 。

 

dt.getFullYear(); // 2017

dt.getMonth(); //1实际为2月份(月份从0开始计算)

dt.getDate(); // 25

dt.getHours(); // 15

dt.getMinutes(); // 36

dt.getSeconds(); // 21

dt.getMilliseconds(); // 125

dt.getDay(); //

dt.getTime(); // 返回Date对象与‘1970/01/01 00:00:00‘之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00‘)

 

 

2、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对象的毫秒值。

var dt = new Date();

dt.setFullYear(2016); // => 2016

dt.setMonth(11); // 11实际为12月份(月份从0开始计算)

dt.setDate(25); // 25

dt.setHours(15); //15

dt.setMinutes(30); // 30

dt.setSeconds(40); // 40

dt.setMilliseconds(333); //333

console.log(dt); // 2016年12月25日 15点30分40秒 333毫秒

 

3、to方法

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‘)

 

var dt = new Date();

console.log(dt.toString()); // Tue Dec 25 2017 20:56:11 GMT+0800 (中国标准时间) console.log(dt.toLocaleString()); //2017年12月25日 下午8:56:11 

console.log(dt.toDateString()); // Tue Dec 23 2014

console.log(dt.toLocaleDateString()); //  2017年12月25日 

console.log(dt.toTimeString()); //20:56:11 GMT+0800 (中国标准时间) console.log(dt.toLocaleTimeString()); //下午8:56:11

console.log(dt.valueOf()); //返回Date对象与‘1970/01/01 00:00:00‘之间的毫秒值(北京时间的时区为东8区,起点时间实际为:‘1970/01/01 08:00:00‘)

Date

标签:日期   自己   相同   16px   gets   字符   valueof   时间   一周   

原文地址:http://www.cnblogs.com/YCxiaoyang/p/7131605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!