在Javascript中OADate到毫秒时间戳 [英] OADate to Milliseconds timestamp in Javascript

查看:241
本文介绍了在Javascript中OADate到毫秒时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做的恰恰相反:

javascript中的DateTime.ToOADate()相当于什么?

创建一个毫秒日期(毫秒数)自1970年1月1日起来自OADate(1899年12月30日以来的天数为双倍价值)

Create a millisecond date (number of milliseconds since 1/1/1970) from a OADate (number of days since 30 dec 1899 as a double value)

我的猜测是我可以这样做:

my guess is I can do like this :

this.unixTimeStampFromOADate = function( OADateFloat)
{
        var oaDateFor1970 = ?? ; //what's the value for 1/1/1970 in OADate format ?
        var delta = Math.floor(OADateFloat - oaDateFor1970);

        return delta*(1000*60*60*24); 
}

所以如果我是对的,我需要1/1的价值/ 1970年采用OADate格式。
如果我错了,你能建议另一种转换方法吗?

so if I'm right, I need the value for 1/1/1970 in OADate format. if I'm wrong, please can you suggest another conversion method ?

推荐答案

Javascript日期使用时间值是自1970-01-01T00:00:00Z以来的毫秒数。日期1899-12-30的时间值是-2209197600000。

Javascript dates use a time value that is milliseconds since 1970-01-01T00:00:00Z. The time value for the date 1899-12-30 is -2209197600000.

要获得此后的日期,请在今天的午夜获取午夜的毫秒数,减去它从OA时代开始,在一天内除以ms并获得绝对值。请注意,时间值都是UTC,因此会考虑夏令时,闰年等。

To get the days since then, get the milliseconds for midnight at the start of today, subtract it from the OA epoch, divide by the ms in one day and get the absolute value. Note that the time values are all UTC so daylight saving, leap years, etc. are accounted for.

var epoch = new Date(1899, 11, 30); // 1899-12-30T00:00:00
var now = new Date();               // 2013-03-22T<current time>
now.setHours(0,0,0,0)               // 2013-03-22T00:00:00

var oaDate  = Math.abs((epoch - now) / 8.64e7); // 41355 for 2013-03-22

您可以针对某些日期对其进行测试这里(请注意,那些日期是令人困惑的美国m / d / yy格式)。

You can test it against some dates here (note that those dates are in the confusing US m/d/yy format).

抱歉,倒退了。以下是一些双向功能。

Sorry, got the sense backwards. Here are some functions to go both ways.

还花了一些时间来解决它所说的OLE自动化日期实现为一个浮点数,其积分成分是午夜之前或之后的天数,1899年12月30日实际上是指在1899-12-30 00:00:00或之后,小数部分代表当天的时间除以24。换句话说,虽然1899-12-29 00:00:00是-1,但是'899-12-29 06:00:00的值是-1.25,而不是-0.75。

Also took some time to work out that where it says "OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899" actually means on or after 1899-12-30 00:00:00 and that the "fractional component represents the time on that day divided by 24". In other words, while 1899-12-29 00:00:00 is -1, the value for `899-12-29 06:00:00 is -1.25, not -0.75.

无论如何,这些函数现在似乎都有效,但请彻底测试:

Anyhow, these functions seem to work now, but please test thoroughly:

var toOADate = (function () {
  var epoch = new Date(1899,11,30);
  var msPerDay = 8.64e7;

  return function(d) {
    var v = -1 * (epoch - d)/msPerDay;

    // Deal with dates prior to 1899-12-30 00:00:00
    var dec = v - Math.floor(v);

    if (v < 0 && dec) {
      v = Math.floor(v) - dec;
    }

    return v;
  }
}());


var fromOADate = (function() {
  var epoch = new Date(1899,11,30);
  var msPerDay = 8.64e7;

  return function(n) {
    // Deal with -ve values
    var dec = n - Math.floor(n);

    if (n < 0 && dec) {
      n = Math.floor(n) - dec;
    }

    return new Date(n*msPerDay + +epoch);
  }
}());

var now = new Date();
var oaNow = toOADate(now);
var now2 = fromOADate(oaNow);

alert('Today: ' + now + '\nOADate: ' + oaNow + '\noaNow to Date: ' + now2);

OADate 令人困惑,尤其是负数的处理方式。

The specification for OADate is confusing, particularly the way negative numbers are handled.

更新版本的函数,使用本地日期值。

Updated version of functions, use local date values.

/* Convert a Microsoft OADate to ECMAScript Date
** Treat all values as local.
** @param {string|number} oaDate - OADate value
** @returns {Date}
*/
function dateFromOADate (oaDate) {
  // Treat integer part is whole days
  var days = parseInt(oaDate);
  // Treat decimal part as part of 24hr day, always +ve
  var ms = Math.abs((oaDate - days) * 8.64e7);
  // Add days and add ms
  return new Date(1899, 11, 30 + days, 0, 0, 0, ms);
}


/* Convert an ECMAScript Date to a Microsoft OADate
** Treat all dates as local.
** @param {Date} date - Date to convert
** @returns {Date}
*/
function dateToOADate (date) {
  var temp = new Date(date);
  // Set temp to start of day and get whole days between dates,
  var days = Math.round((temp.setHours(0,0,0,0) - new Date(1899, 11, 30)) / 8.64e7);
  // Get decimal part of day, OADate always assumes 24 hours in day
  var partDay = Math.abs((date - temp) % 8.64e7) / 8.64e7;
  return (days + partDay).toFixed(10);
}

var now = new Date();
var x = dateToOADate(now);
console.log('Now: ' + now.toString());
console.log('As an OADate: ' + x);
console.log('Back to date: ' + dateFromOADate(x).toString());

这篇关于在Javascript中OADate到毫秒时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆