Skip to content

Instantly share code, notes, and snippets.

@lbj96347
Created May 3, 2012 08:19
Show Gist options
  • Save lbj96347/2584284 to your computer and use it in GitHub Desktop.
Save lbj96347/2584284 to your computer and use it in GitHub Desktop.
Js time checking
/*
* 检查数据库时间离现在多久远了
* 加载该js文件
* 传入参数 dbtime
* example : var dbtime = '2012-04-08 15:57:07';
* 运行函数 getTimeCheck(dbtime)
* 最后输出变量getTimeCheck
* 20120916更新-使用继承的方式来输出结果值(尾部是demo,自行去掉demo使用)
*
*/
var finalTime;
var count = 0;
var dbtime = Array();
dbtime[0] = "2012-09-14 23:25:21";
dbtime[1] = "2012-08-14 23:25:21";
dbtime[2] = "2012-09-14 22:25:21";
dbtime[3] = "2012-09-14 23:25:21";
dbtime[4] = "2012-09-14 11:25:21";
dbtime[5] = "2012-07-14 23:22:21";
var getTimeCheck = function (dbtime){
this.time = dbtime;
}
// 定义getTimeCheck的原型,原型中的属性可以被自定义对象引用
getTimeCheck.prototype = {
getTime: function() {
var pasttime = [
this.time.split(" ")[0].split("-")[0].substring(2),
this.time.split(" ")[0].split("-")[1],
this.time.split(" ")[0].split("-")[2],
this.time.split(" ")[1].split(":")[0],
this.time.split(" ")[1].split(":")[1],
this.time.split(" ")[1].split(":")[2]
];
//获取javascript 时间
var today = new Date();
var nowtime = [
today.getYear()-100,
today.getMonth()+1,
today.getDate(),
today.getHours(),
today.getMinutes(),
today.getSeconds()
];
//console.log(nowtime[0] - pasttime[0]);
var timename = ['年','个月','日','小时','分钟','秒'];
//console.log(timename[0]);
for ( i = 0 ; i < 6 ; i++ ){
if ( pasttime[i] != nowtime[i]){
var checktime = nowtime[i] - pasttime[i];
if( checktime > 0 ){
finalTime = checktime + timename [i] + '前';
return finalTime;
}
}
}
//return this.time;
}
}
for( var x in dbtime ){
var hello = new getTimeCheck(dbtime[x]);
console.log(hello.getTime());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment