JS 时间获取函数

mac2026-09-01  10

new Date() //Date首字母必须大写

主要的初始化如下

new Date()

无参数的格式,返回使用该函数时的时间

new Date(milliseconds)

返回从 1970 年 1 月 1 日至今的毫秒数(1ss=1000ms)

new Date(dateString)

dateString为时间格式的字符串 其中dateString的格式可以为: 1、“月 日 年 小时:分钟:秒:毫秒” 2、“月 日 年 小时:分钟:秒” 3、“月 日 年 小时:分钟” 4、“月 日 年” 5、“月 日” 6、“年” //输入为数值 7、“月” //输入的数值为1-12时为月份 8、“年 月 日 小时:分钟:秒:毫秒” 9、“年 月 日 小时:分钟:秒” 10、“年 月 日 小时:分钟” 11、“年 月 日”

** 值得注意 1、除第七个格式外,月份可以是数值,也可以是英文对应的月份。 2、空格可由英文格式的某些字符代替 3、0<月<13;0<日<32;0<=小时<24;0<=分钟,秒<60;0<=毫秒<1000。 **

new Date(year, month, day, hours, minutes, seconds, milliseconds)

该初始化方式的值均为number,且day,hours,minutes,seconds,milliseonds参数值都可以省略,且遵循如下规则: ** 0<=month<=11 0<=day<=31 0<=hours<=24 0<=minutes<=59 0<=seconds<=59 milliseconds>=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 日至今的毫秒数。

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 对象。

toTimeString() 把 Date 对象的时间部分转换为字符串。 toDateString() 把 Date 对象的日期部分转换为字符串。 toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。 toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。 toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 valueOf() 返回 Date 对象的原始值。

最新回复(0)