JavaScript Date(日期)对象

来源:互联网 发布:nba体测数据之最 编辑:程序博客网 时间:2024/05/22 03:25

1 Date构造函数

Date对象有7个构造函数,这7个构造函数根据参数可以分为4类。①没有参数的构造函数,默认返回当前日期。②参数为一个数值的构造函数,则将数值视为距离1970年1月1日的毫秒值,转换为日期。③参数为一个字符串,则将字符串视为日期的字符串表示,转换为日期。④多个参数的构造函数,精确指定年月日时分秒。

//Date()//无参数构造函数,默认当前时间var date = new Date();//Date(milliseconds)//milliseconds   距离1970年1月1日的毫秒数var milliseconds = 1351572205906;var date = new Date(milliseconds);//Date(datestring)//datestring     日期的字符串格式,常用格式2010-10-5 12:00:00(在google浏览器中有效)、10/5/2012 12:00:00、2010/10/5 12:00:00、Oct 5 2012 12:00:00var datestring = '2010/10/5 12:00:00';var date = new Date(datestring);//Date(year, month) //year           四位数的年份,如果取值为0~99之间,则year = year + 1900。//month          取值0~11之间,代表1月到12月。var year = 2010, month = 10;var date = new Date(year, month); //2010年11月//Date(year, month, day)//year           四位数的年份,如果取值为0~99之间,则year = year + 1900。//month          取值0~11之间,代表1月到12月。//day            取值1~31之间的天数。var year = 2010, month = 10, day = 1;var date = new Date(year, month, day); //2010年11月1日//Date(year, month, day, hours)//year           四位数的年份,如果取值为0~99之间,则year = year + 1900。//month          取值0~11之间,代表1月到12月。//day            取值1~31之间的天数。//hours          取值0~23之间的小时。var year = 2010, month = 10, day = 1, hours = 10;var date = new Date(year, month, day, hours); //2010年11月1日 10:00:00//Date(year, month, day, hours, minutes)//year           四位数的年份,如果取值为0~99之间,则year = year + 1900。//month          取值0~11之间,代表1月到12月。//day            取值1~31之间的天数。//hours          取值0~23之间的小时。//minutes        取值0~59之间的分钟数。var year = 2010, month = 10, day = 1, hours = 10, minutes = 30;var date = new Date(year, month, day, hours, minutes); //2010年11月1日 10:30:00//Date(year, month, day, hours, minutes, seconds)//year           四位数的年份,如果取值为0~99之间,则year = year + 1900。//month          取值0~11之间,代表1月到12月。//day            取值1~31之间的天数。//hours          取值0~23之间的小时。//minutes        取值0~59之间的分钟数。//seconds        取值0~59之间的秒数。var year = 2010, month = 10, day = 1, hours = 10, minutes = 30, seconds = 45;var date = new Date(year, month, day, hours, minutes, seconds); //2010年11月1日 10:30:45//Date(year, month, day, hours, minutes, seconds, microseconds)//year           四位数的年份,如果取值为0~99之间,则year = year + 1900。//month          取值0~11之间,代表1月到12月。//day            取值1~31之间的天数。//hours          取值0~23之间的小时。//minutes        取值0~59之间的分钟数。//seconds        取值0~59之间的秒数。//microseconds   取值0~999之间的毫秒数。var year = 2010, month = 10, day = 1;var hours = 10, minutes = 30, seconds = 45, microseconds = 500;var date = new Date(year, month, day, hours, minutes, seconds, microseconds); //2010年11月1日 10:30:45:500

2 Date对象常用函数

var date = new Date('2010-10-1 12:30:00'); //2010年10月1日 12时30分00秒//获取一个月中的某一天(1 ~ 31)。date.getDate();         //返回1//获取一周中的某一天(0~6),0代表星期天,6代表星期六。date.getDay();          //返回5,代表星期五//获取月份(0~11),代表1月到12月。date.getMonth();        //返回9,代表10月//获取四位数字的年份。date.getFullYear();     //返回2010//获取date年份到1900年之间的差值。date.getYear();         //返回110,实际年份是1900 + 110 = 2010//获取小时(0~23)。date.getHours();        //返回12//获取分钟(0~59)。date.getMinutes();      //返回30//获取秒数(0~59)。date.getSeconds();      //返回0//获取毫秒(0~999)。date.getMilliseconds(); //返回0//获取1970年1月1日至date的毫秒数。date.getTime();         //返回1285907400000//获取本地时间与格林威治标准时间(GMT)的分钟差。date.getTimezoneOffset(); //-480(本地时区北京东8区)//设置月的某一天(1~31)。date.setDate(10);       //2010年10月10日 12时30分0秒0毫秒//设置月份(0~11)。date.setMonth(5);       //2010年6月10日 12时30分0秒0毫秒//设置四位数字年份。date.setFullYear(2012); //2012年6月10日 12时30分0秒0毫秒//设置小时(0~23)。date.setHours(14);      //2012年6月10日 14时30分0秒0毫秒//设置分钟(0~59)。date.setMinutes(20);    //2012年6月10日 14时20分0秒0毫秒//设置秒(0~59)。date.setSeconds(45);    //2012年6月10日 14时20分45秒0毫秒//设置毫秒(0~999)。date.setMilliseconds(500);    //2012年6月10日 14时20分45秒500毫秒//根据毫秒数设置,毫秒数为1970年1月1日到指定日期的数值。date.setTime(1356841800000);  //2010年12月30日 12时30分0秒0毫秒//把Date转换为字符串。date.toString();        //Sun Dec 30 2012 12:30:00 GMT+0800 (中国标准时间)//把Date的时间部分转换为字符串。date.toTimeString();    //12:30:00 GMT+0800 (中国标准时间)//把Date的日期部分转换为字符串。date.toDateString();    //Sun Dec 30 2012//根据本地时间格式,把Date转换成字符串。date.toLocaleString();  //Sun Dec 30 2012 12:30:00 GMT+0800 (中国标准时间)

3 Date对象静态函数

//now()   返回1970年1月1日至今的毫秒数。注:该函数在IE6、IE7、IE8中没有。Date.now();//parse(datestring)     返回指定日期到1970年1月1日的毫秒数。//datestring            日期的字符串格式。var datestring = '2010-10-10 12:00:00';Date.parse(datestring);//UTC(datestring)       根据世界时返回1970年1月1日到指定日期的毫秒数。//datestring            日期的字符串格式。var datestring = '2010-10-10 12:00:00';Date.UTC(datestring);


原创粉丝点击