javascript时间差插件分享,供大家参考,具体内容如下
Html如下:
<html> <head> <title></title> <script src="/UploadFiles/2021-04-02/TimeDifference.js">TimeDifference.js代码如下:
/** * 函数使用说明: * 1、直接调用函数 TimeDifference() * 返回说明: 返回距离当前的时间差 * */ function timeDifference(tmpTime) { var mm=1000;//1000毫秒 代表1秒 var minute = mm * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var ansTimeDifference=0;//记录时间差 var tmpTimeStamp = tmpTime "/")) : new Date().getTime();//将 yyyy-mm-dd H:m:s 进行正则匹配 var nowTime = new Date().getTime();//获取当前时间戳 var tmpTimeDifference = nowTime - tmpTimeStamp;//计算当前与需要计算的时间的时间戳的差值 if (tmpTimeDifference < 0) { //时间超出,不能计算 alert("开始日期大于结束日期,计算失败!"); return 0; } /** * 通过最开始强调的各个时间段用毫秒表示的数值,进行时间上的取整,为0的话,则没有到达 * */ var DifferebceMonth = tmpTimeDifference / month; //进行月份取整 var DifferebceWeek = tmpTimeDifference / (7 * day);//进行周取整 var DifferebceDay = tmpTimeDifference / day;//进行天取整 var DifferebceHour = tmpTimeDifference / hour;//进行小时取整 var DifferebceMinute = tmpTimeDifference / minute;//进行分钟取整 if (DifferebceMonth >= 1) { return tmpTime; //大于一个月 直接返回时间 } else if (DifferebceWeek >= 1) { ansTimeDifference= parseInt(DifferebceWeek) + "个星期前"; } else if (DifferebceDay >= 1) { ansTimeDifference = parseInt(DifferebceDay) + "天前"; } else if (DifferebceHour >= 1) { ansTimeDifference = parseInt(DifferebceHour) + "个小时前"; } else if (DifferebceMinute >= 1) { ansTimeDifference = parseInt(DifferebceMinute) + "分钟前"; } else { ansTimeDifference = "刚刚"; } return ansTimeDifference; }结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《中央民族乐团-古筝传奇》1CD[MP3][253.1MB]
- 江玲.1989-这样你才爱我,是吗?【新作有声】【WAV+CUE】
- 郭美美.2010-我是郭美美【华纳】【WAV+CUE】
- 黄心懋1991-轻忧郁【滚石】【WAV+CUE】
- 群星.2000-大地金曲世纪回顾2CD【大地】【WAV+CUE】
- 王菲.1996-浮躁(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 群星.2022-传家电视剧原声带【东阳欢娱】【FLAC分轨】
- 《摇滚教父 伍佰黄金精选 2CD》 [WAV+CUE][1GB]
- 《凤凰传奇 我从草原来》[WAV/分轨][400MB]
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]