TimeDifference.js获取时间差插件

本文介绍了一个使用JavaScript编写的计算时间差的插件,能够帮助开发者轻松计算两个时间点之间的差距,并提供人性化的表述方式,如“几分钟前”等。
[url]http://www.oschina.net/p/timedifference[/url]

/** 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;
}
### 使用 `time.h` 头文件获取当前时间 在 C/C++ 中,可以通过 `<time.h>` 或 `<ctime>` 头文件中的函数来操作时间和日期。以下是几个常用的函数及其用途: #### 函数说明 1. **`time()`**: 获取当前的日历时间(Calendar Time),并将其存储为 `time_t` 类型的值。 2. **`localtime()`**: 将 `time_t` 类型的时间转换为本地时间,并返回指向 `struct tm` 的指针。 3. **`gmtime()`**: 将 `time_t` 类型的时间转换为 UTC 时间,并返回指向 `struct tm` 的指针[^2]。 4. **`asctime()` 和 `ctime()`**: 这两个函数可以将时间转换为可读的字符串形式。区别在于 `asctime()` 接受 `struct tm*` 参数,而 `ctime()` 接受 `time_t` 参数[^3]。 #### 示例代码 以下是一个完整的示例,展示如何使用这些函数获取当前时间并打印出来: ```cpp #include <iostream> #include <iomanip> // std::put_time #include <ctime> int main() { // Step 1: 获取当前日历时间 time_t now = time(nullptr); // Step 2: 转换为本地时间 struct tm local_tm = *localtime(&now); // Step 3: 打印时间为标准格式 std::cout << "Current Local Time: " << std::put_time(&local_tm, "%Y-%m-%d %H:%M:%S") << std::endl; // 可选:UTC 时间 struct tm utc_tm = *gmtime(&now); std::cout << "Current UTC Time: " << std::put_time(&utc_tm, "%Y-%m-%d %H:%M:%S") << std::endl; return 0; } ``` #### 输出解释 上述代码会输出当前的本地时间和 UTC 时间,格式为 `YYYY-MM-DD HH:MM:SS`。 - `%Y`: 年份(四位数) - `%m`: 月份(两位数) - `%d`: 日(两位数) - `%H`: 小时(24小时制,两位数) - `%M`: 分钟(两位数) - `%S`: 秒(两位数) #### 计算时间差 如果需要计算两个时间点之间的差异,可以使用 `difftime(time_t time2, time_t time1)` 函数[^4]。例如: ```cpp #include <iostream> #include <ctime> int main() { time_t start = time(nullptr); // 假设这里有一些耗时的操作... sleep(2); // 模拟延迟 time_t end = time(nullptr); double difference = difftime(end, start); std::cout << "Elapsed time: " << difference << " seconds." << std::endl; return 0; } ``` 此代码片段展示了如何测量两段时间之间的差距,并以秒为单位显示结果。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值