js Date总结

来源:互联网 发布:淘宝视频怎么发布 编辑:程序博客网 时间:2024/05/17 02:18

时区的概念

世界被分为24个时区,相邻的时区之间相差1小时
GMT或UTC表示 世界时间(0时区的标准时间)
UTC+0800 表示北京时间(与世界时间相差+08:00的时间,即多8个小时的东8区的标准时间)

Date类

js中通过Date类来处理时间。
new Date(); 构造一个Date类,默认表示本地时区的当前时间(根据操作系统的时间)。Date通过年,月,日,时,分,秒,毫秒和与世界时间的时差,来表示一个特定的时间。
new Date(timestamp); 通过一个unix时间戳构造一个Date类,时区为本地时区
Date.UTC(year, month, day, hours, minutes, seconds, ms); 用世界时间(0时区时间)来构造一个Date

//以下成员函数以本地时区来获取和设置时间
getDate()     从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay()     从 Date 对象返回一周中的某一天 (0 ~ 6)。
getMonth()     从 Date 对象返回月份 (0 ~ 11)。
getFullYear()     从 Date 对象以四位数字返回年份。
getHours()     返回 Date 对象的小时 (0 ~ 23)。
getMinutes()     返回 Date 对象的分钟 (0 ~ 59)。
getSeconds()     返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds()     返回 Date 对象的毫秒(0 ~ 999)。
getTime()     返回 1970 年 1 月 1 日至今的毫秒数(unix时间戳)。
getTimezoneOffset()     返回本地时间与格林威治标准时间 (GMT) 的分钟差。
setDate()     设置 Date 对象中月的某一天 (1 ~ 31)。
setMonth()     设置 Date 对象中月份 (0 ~ 11)。
setFullYear()     设置 Date 对象中的年份(四位数字)。
setYear()     请使用 setFullYear() 方法代替。
setHours()     设置 Date 对象中的小时 (0 ~ 23)。
setMinutes()     设置 Date 对象中的分钟 (0 ~ 59)。
setSeconds()     设置 Date 对象中的秒钟 (0 ~ 59)。
setMilliseconds()     设置 Date 对象中的毫秒 (0 ~ 999)。
setTime()     以毫秒设置 Date 对象(unix时间戳)。

//以下成员函数以世界时区(0时区)来获取和设置时间
getUTCDate()     根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
getUTCDay()     根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
getUTCMonth()     根据世界时从 Date 对象返回月份 (0 ~ 11)。
getUTCFullYear()     根据世界时从 Date 对象返回四位数的年份。
getUTCHours()     根据世界时返回 Date 对象的小时 (0 ~ 23)。
getUTCMinutes()     根据世界时返回 Date 对象的分钟 (0 ~ 59)。
getUTCSeconds()     根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
getUTCMilliseconds()     根据世界时返回 Date 对象的毫秒(0 ~ 999)。
setUTCDate()     根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCMonth()     根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCFullYear()     根据世界时设置 Date 对象中的年份(四位数字)。
setUTCHours()     根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMinutes()     根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCSeconds()     根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。
setUTCMilliseconds()     根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。

//用于转换的成员函数
parse()     返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
toSource()     返回该对象的源代码。
toString()     把 Date 对象转换为字符串。(本地时间)
toTimeString()     把 Date 对象的时间部分转换为字符串。
toDateString()     把 Date 对象的日期部分转换为字符串。
toUTCString()     根据世界时,把 Date 对象转换为字符串。
toLocaleString()     根据本地时间格式,把 Date 对象转换为字符串。
toLocaleTimeString()     根据本地时间格式,把 Date 对象的时间部分转换为字符串。
toLocaleDateString()     根据本地时间格式,把 Date 对象的日期部分转换为字符串。
1 0
原创粉丝点击