JavaScript时间对象之常用方法的设置实例
作者:陆荣涛
时间对象常用方法—设置
设置年 setFullYear
作用:单独设置时间对象内的年份信息
语法:时间对象.setFullYear(年份信息)
返回值:返回一个修改后的时间
var time = new Date() console.log(time) //Thu Sep 30 2021 14:24:27 GMT+0800 (中国标准时间) time.setFullYear(2019) console.log(time) //Mon Sep 30 2019 14:24:27 GMT+0800 (中国标准时间)
设置月 setMonth
作用:单独设置时间对象内的月份信息
语法:时间对象.setMonth(月份信息)
返回值:返回一个修改后的时间
注意:0 表示 1 月份, 11 表示 12 月份
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setMonth(11) console.log(time) //Thu Dec 30 2021 14:33:33 GMT+0800 (中国标准时间)
设置天 setDate
作用:单独设置时间对象内的日期信息
语法:时间对象.setDate(日期信息)
返回值:返回一个修改后的时间
月份是1--31
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setDate(11) console.log(time) //Sat Sep 11 2021 14:39:03 GMT+0800 (中国标准时间)
设置小时 setHours
作用:单独设置时间对象内的小时信息
语法:时间对象.setHours(小时信息)
返回值:返回一个修改后的时间
时间是0~~23
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setHours(22) console.log(time) //Thu Sep 30 2021 22:45:59 GMT+0800 (中国标准时间)
设置分钟 setMinutes
作用:单独设置时间对象内的分钟信息
语法:时间对象.setMinutes(分钟信息)
返回值:返回一个修改后的时间
时间是0~~59
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setMinutes(45) console.log(time) //Thu Sep 30 2021 14:45:52 GMT+0800 (中国标准时间)
设置秒 setSeconds
作用:单独设置时间对象内的秒钟信息
语法:时间对象.setSeconds(秒钟信息)
返回值:返回一个修改后的时间
时间是0~~59
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setSeconds(55) console.log(time) //Thu Sep 30 2021 14:52:55 GMT+0800 (中国标准时间)
设置毫秒 setMilliseconds
作用:单独设置时间对象内的毫秒信息
语法:时间对象.setMilliseconds(毫秒信息)
返回值:返回一个修改后的时间
时间是0~~999
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setMilliseconds(1000) console.log(time.setMilliseconds(1000)) //1632985218000
设置时间戳 setTime
作用:用来这是时间戳
语法:时间对象.setTime(毫秒信息)
返回值:返回一个格林威治时间到设置时间的一个时间
var time = new Date() console.log(time) //Thu Sep 30 2021 14:33:33 GMT+0800 (中国标准时间) time.setTime(1632985218000) console.log(time) //Thu Sep 30 2021 15:00:18 GMT+0800 (中国标准时间) time.setTime(2000) console.log(time);//Thu Jan 01 1970 08:00:02 GMT+0800 (中国标准时间)
Date 内对象内的一些其他方法
时间对象的其它方法
toString() :将Date转换为一个'年月日 时分秒'字符串
toLocaleString() :将Date转换为一个'年月日 时分秒'的本地格式字符串
toDateString() :将Date转换为一个'年月日'字符串
toLocaleDateString() :将Date转换为一个'年月日'的本地格式字符串
toTimeString() :将Date转换为一个'时分秒'字符串
toLocaleTimeString() :将Date转换为一个'时分秒'的本地格式字符串
valueOf() :与getTime()一样, 返回一个毫秒数从现在到格林威治时间的毫秒数
// 时间对象的其它方法 var time = new Date(); console.log(time); // Tue Feb 21 2023 15:10:38 GMT+0800 (中国标准时间) console.log(time.()); // Tue Feb 21 2023 15:10:38 GMT+0800 (中国标准时间) console.log(time.toLocaleString()); // 2023/2/21 15:10:38 console.log(time.toDateString()); // Tue Feb 21 2023 console.log(time.toLocaleDateString()); // 2023/2/21 console.log(time.toTimeString()); // 15:10:38 GMT+0800 (中国标准时间) console.log(time.toLocaleTimeString()); // 15:10:38 console.log(time.valueOf()); // 1676963438332
静态方法
Date.now() :返回一个毫秒数从现在到格林威治时间的毫秒数
Date.parse(dateStr) : 把字符串转换为Date对象 ,然后返回此Date对象与格林威治时间的毫秒数
// 静态方法
console.log(Date.now()) // 1676963759879
console.log(Date.parse('2023/2/20 12:00:00')); // 1676865600000
console.log(Date.parse('2023-2-20 12:00:00')); // 1676865600000案例 - 封装时间差函数
// 定一两个时间
var time = new Date()
var time1 = new Date('2021-11-23 00: 00: 00')
// 既然要分装成一个函数 就要先定义一个函数
function diffTime(time, time1) {
// 定义一个空对象
var obj = {}
// 首先我们要拿到传进来的两个时间 但是我们不知道那个是大一点儿的时间 那个是小一点儿的时间
var subMs = Math.abs(time - time1)
// 这里我们先把毫秒转成秒
var sub = Math.ceil(subMs / 1000)
// 计算我们有多少天 多少小时多少分钟和多少秒
var day = parseInt(sub / (24 * 60 * 60))
var hours = parseInt(sub % (24 * 60 * 60) / (60 * 60))
var minutes = parseInt(sub % (60 * 60) / 60)
var seconds = sub % 60
// 接下来就是把我们拿到的添加到对象里面
obj.day = day
obj.hours = hours
obj.minutes = minutes
obj.seconds = seconds
// 最后我们要把这个空对象返回出去
return obj
}
// 首次代码优化
var time1 = new Date()
var time2 = new Date('2021-12-12 00:00:00')
function diffTime(time1, time2) {
var obj = {}
var subMs = Math.abs(time1 - time2)
var sub = Math.ceil(subMs / 1000)
var day = parseInt(sub / (24 * 60 * 60))
var hours = parseInt(sub % (24 * 60 * 60) / (60 * 60))
var minutes = parseInt(sub % (60 * 60) / 60)
var seconds = sub % 60
obj.day = day
obj.hours = hours
obj.minutes = minutes
obj.seconds = seconds
return obj
}
// 使用
var res = diffTime(time, time1)
console.log(res);
// 再次代码优化
var time1 = new Date()
var time2 = new Date('2021-12-12 00:00:00')
function diffTime(time1, time2) {
var sub = Math.ceil(Math.abs(time1 - time2) / 1000)
return {
day: parseInt(sub / (24 * 60 * 60)),
hours: parseInt(sub % (24 * 60 * 60) / (60 * 60)),
minutes: parseInt(sub % (60 * 60) / 60),
seconds: sub % 60
}
}
// 使用
var res = diffTime(time1, time2)
console.log(res);
// 代码优化
function diffTime(time, time1) {
var sub = Math.ceil(Math.abs(time - time1) / 1000)
return obj = {
day: parseInt(sub / (24 * 60 * 60)),
hours: parseInt(sub % (24 * 60 * 60) / (60 * 60)),
minutes: parseInt(sub % (60 * 60) / 60),
seconds: sub % 60
}
}
var res = diffTime(time, time1)
console.log(res);以上就是JavaScript时间对象之常用方法的设置实例的详细内容,更多关于JavaScript时间对象设置的资料请关注脚本之家其它相关文章!
