1 Star 0 Fork 13

skykainls / TimeDifference.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

请直接下载附件

/** Created by postbird on 2016/4/1.  ...*/
/**
 *      @postbird
 *      1、本插件采用js编写,可直接将函数复制到个人js文件,减少get请求数
 *      2、author:powered by postbird
 *      3、email: ptbird@yeah.net
 *      4、site:http://www.ptbird.cn
 *      5、license : MIT
 * */
/**
 *  函数使用说明:
 *      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 ? Date.parse(tmpTime.replace(/-/gi, "/")) : 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;
}
The MIT License (MIT) Copyright (c) 2016 postbird Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

JS获取时间差插件,显示为几分钟前、几小时前等 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/skykain/TimeDifference.js.git
git@gitee.com:skykain/TimeDifference.js.git
skykain
TimeDifference.js
TimeDifference.js
master

搜索帮助