js关于时间的方法

JavaScript 提供了多种与时间和日期相关的方法,这些方法主要包含在 Date 对象中。以下是一些常用的 Date 对象方法和属性:

构造方法
new Date(): 创建一个新的日期对象,如果省略参数,则根据当前日期和时间来初始化。
new Date(value): 根据提供的日期时间戳(毫秒为单位)初始化。
new Date(dateString): 根据提供的日期时间字符串初始化。
new Date(year, monthIndex[, day[, hour[, minutes[, seconds[, milliseconds]]]]]): 根据提供的参数初始化。

属性
Date.prototype.getFullYear(): 返回日期的年份(四位数)。
Date.prototype.getMonth(): 返回日期的月份(0-11)。
Date.prototype.getDate(): 返回日期的天数(1-31)。
Date.prototype.getHours(): 返回日期的小时数(0-23)。
Date.prototype.getMinutes(): 返回日期的分钟数(0-59)。
Date.prototype.getSeconds(): 返回日期的秒数(0-59)。
Date.prototype.getMilliseconds(): 返回日期的毫秒数(0-999)。
Date.prototype.getDay(): 返回日期的星期几(0-6,其中 0 表示星期日)。
Date.prototype.getTime(): 返回日期时间的数值表示,即从1970年1月1日00:00:00 UTC至现在的毫秒数。

方法
Date.prototype.setFullYear(year[, month[, day]]): 设置日期的年份。
Date.prototype.setMonth(monthIndex[, day]): 设置日期的月份。
Date.prototype.setDate(day): 设置日期的天数。
Date.prototype.setHours(hour[, min[, sec[, ms]]]): 设置日期的小时数。
Date.prototype.setMinutes(min[, sec[, ms]]): 设置日期的分钟数。
Date.prototype.setSeconds(sec[, ms]): 设置日期的秒数。
Date.prototype.setMilliseconds(ms): 设置日期的毫秒数。
Date.prototype.setTime(time): 设置日期时间的数值表示。

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值