JavaScript中的Date类型-计算时间差
2012-02-19 21:33
288 查看
接触JavaScript不久,今日想写一个简单的秒表练练手,于是就想当然的写下了如下代码:
var date = new Date(currTime.getTime() - beginTime.getTime());
// currTime.getTime()=1329655993149 beginTime.getTime()=1329655991421
var hour = date.getHours();
结果比较出乎我的意料,hour变量的值并不是我所期望的,hour的值是8。currTime和beginTime变量实际的时间差也就2秒钟哪来的8个小时?!经过一番思索和搜索,我发现原因是因为在JavaScript中并没有类似于TimeSpan这种类型的变量,而DateTime类型又是从1970年1月1日开始计时的。
所以我生成的那个date变量其实是1970年1月1日0时0分1秒728毫秒,Date所存的毫秒数有是独立于时区的,是一个UTC+0的时间。当我的浏览器调用getHours()函数时,将这个时间转换成我本地设置的时区也就是UTC+8,所以得到的时间是1970年1月1日8时0分1秒728毫秒。这就是为什么会得到8的原因了。
JavaScript中如果需要计算两个时间的差只能乖乖的通过将两个时间相减得到一个毫秒差,然后手动计算得到。写了一个timeSpan伪类来将时间差转换为有意义的时间。
function timeSpan(msec) {
var milliseconds = msec;
this.getDays = function () {
return Math.floor(this.getHours() / 24);
}
this.getHours = function () {
return Math.floor(this.getMinutes() / 60);
}
this.getMinutes = function () {
return Math.floor(this.getSeconds() / 60);
}
this.getSeconds = function () {
return Math.floor(milliseconds / 1000);
}
//以下是获取时间间隔的具体部分?
this.getMillisecondPart = function () {
return milliseconds - this.getSeconds() * 1000;
}
this.getSecondPart = function () {
return this.getSeconds() - 60 * this.getMinutes();
}
this.getMinutePart = function () {
return this.getMinutes() - 60 * this.getHours();
}
this.getHourPart = function () {
return this.getHours() - 24 * this.getDays();
}
}
var date = new Date(currTime.getTime() - beginTime.getTime());
// currTime.getTime()=1329655993149 beginTime.getTime()=1329655991421
var hour = date.getHours();
结果比较出乎我的意料,hour变量的值并不是我所期望的,hour的值是8。currTime和beginTime变量实际的时间差也就2秒钟哪来的8个小时?!经过一番思索和搜索,我发现原因是因为在JavaScript中并没有类似于TimeSpan这种类型的变量,而DateTime类型又是从1970年1月1日开始计时的。
所以我生成的那个date变量其实是1970年1月1日0时0分1秒728毫秒,Date所存的毫秒数有是独立于时区的,是一个UTC+0的时间。当我的浏览器调用getHours()函数时,将这个时间转换成我本地设置的时区也就是UTC+8,所以得到的时间是1970年1月1日8时0分1秒728毫秒。这就是为什么会得到8的原因了。
JavaScript中如果需要计算两个时间的差只能乖乖的通过将两个时间相减得到一个毫秒差,然后手动计算得到。写了一个timeSpan伪类来将时间差转换为有意义的时间。
function timeSpan(msec) {
var milliseconds = msec;
this.getDays = function () {
return Math.floor(this.getHours() / 24);
}
this.getHours = function () {
return Math.floor(this.getMinutes() / 60);
}
this.getMinutes = function () {
return Math.floor(this.getSeconds() / 60);
}
this.getSeconds = function () {
return Math.floor(milliseconds / 1000);
}
//以下是获取时间间隔的具体部分?
this.getMillisecondPart = function () {
return milliseconds - this.getSeconds() * 1000;
}
this.getSecondPart = function () {
return this.getSeconds() - 60 * this.getMinutes();
}
this.getMinutePart = function () {
return this.getMinutes() - 60 * this.getHours();
}
this.getHourPart = function () {
return this.getHours() - 24 * this.getDays();
}
}
相关文章推荐
- javascript时间戳转换为date类型
- 深入JavaScript与.NET Framework中的日期时间(2):JavaScript中的Date类型(上)
- 用My97DatePicker日期框架取得时间后转换为可在javascript中比较的Date类型
- 深入JavaScript与.NET Framework中的日期时间(2):JavaScript中的Date类型(上)
- 第五部份 01 深入JavaScript与.NET Framework中的日期时间 JavaScript中的Date类型
- java计算一个Date(TimeStamp)与一个格式化的String类型时间的相差天数或者相差时间
- javascript引用类型之时间Date和数组Array
- javascript学习之日期 字符串(14)—— 时间和日期 Date类型
- 深入JavaScript与.NET Framework中的日期时间(3):JavaScript中的Date类型(中)
- JS中String类型转换Date类型 并 计算时间差
- 深入JavaScript与.NET Framework中的日期时间(3):JavaScript中的Date类型(中)
- javascript引用类型之时间Date和数组Array
- 深入JavaScript与.NET Framework中的日期时间(3):JavaScript中的Date类型(中)
- 深入JavaScript与.NET Framework中的日期时间(2):JavaScript中的Date类型(上)
- 深入JavaScript与.NET Framework中的日期时间(3):JavaScript中的Date类型(中)
- javascript 关于Date 时间类型 处理方法
- Oracle时间类型date,timestamp时间差计算
- 深入JavaScript与.NET Framework中的日期时间(2):JavaScript中的Date类型(上)
- 深入JavaScript与.NET Framework中的日期时间(3):JavaScript中的Date类型(中)
- PLS_INTEGER类型与timestamp类型、date、及时间函数