js中时间和时间戳的相互转换

js中时间和时间戳的相互转换一 时间转时间戳 1 var time1 new Date data getTime 1000 2 var time2 new Date data valueOf 1000 3 var time3 Date parse data 备注 data 是需要转换的时间 第一 第二种

大家好,我是讯享网,很高兴认识大家。

一、时间转时间戳

1、var time1=new Date(data).getTime()/ 1000

2、var time2 = new Date(data).valueOf()/ 1000;

3、var time3 = Date.parse(data);

备注:data是需要转换的时间

第一、第二种:会精确到毫秒,第三种:只能精确到秒,毫秒用000替代,注意:获取到的时间戳除以1000就可获得Unix时间戳,就可传值给后台得到。

除以100表示精确到秒,具体看需求场景

二、时间戳转化为时间

页面需要展示开始时间和结束时间,此时,需要把时间戳转化为正常年月日的格式:

// 时间戳转为正常时间的公共方法,当然你也可以加上小时、分、秒 filterTime(time) { const date = new Date(time) const Y = date.getFullYear() const M = date.getMonth() + 1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1 const D = date.getDate() return `${Y}-${M}-${D}` }, // 在点击确认添加之后,调用filterTime方法,赋值给当前数据即可,下边是小时、分、秒 const H = date.getHours() // 小时 const M = date.getMinutes() // 分钟 const S = date.getSeconds() // 秒 // 下边这个方法是判断时间是否小于10,小于10 前边加0,更加规范一些,看自己需求 function isAddZero(time) { let str = '' if(time < 10) { str = '0' + time }else{ str = time.toString() } return str // 方法二 let str = '' str = time < 10 ? '0' + time : time.toString() return str }

讯享网

三、补充资料

Date() 返回当日的日期和时间。 

getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。 

getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。 

getMonth() 从 Date 对象返回月份 (0 ~ 11)。 

getFullYear() 从 Date 对象以四位数字返回年份。 

getYear() 请使用 getFullYear() 方法代替。 

getHours() 返回 Date 对象的小时 (0 ~ 23)。 

getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。 

getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。 

getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。 

getTime() 返回 1970 年 1 月 1 日至今的毫秒数。 

getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。 

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)。 


讯享网

parse() 返回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 对象。 

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)。 

toSource() 返回该对象的源代码。 

toString() 把 Date 对象转换为字符串。 

toTimeString() 把 Date 对象的时间部分转换为字符串。 

toDateString() 把 Date 对象的日期部分转换为字符串。 

toGMTString() 请使用 toUTCString() 方法代替。 

toUTCString() 根据世界时,把 Date 对象转换为字符串。 

toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。 

toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 

UTC() 根据世界时返回 1997 年 1 月 1 日 到指定日期的毫秒数。valueOf() 返回 Date 对象的原始值。

小讯
上一篇 2025-03-10 19:21
下一篇 2025-01-29 13:02

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/115840.html