如何处理MySQL到Javascript日期格式?

时间:2016-02-12 18:16:36

标签: javascript mysql datetime

设定:  带有UpdateTracker表的MySQL数据库,其中包含名为ANY的列。

我运行它来获取我的数据集:

RefData_UpdateTracker.request = new AP.MySQL.Request();

RefData_UpdateTracker.request.query("SELECT * FROM Common_RefData.RefData_UpdateTracker"); 

试图根据日期比较做一些条件,但没有一个工作..所以我只是设置它输出数据集:

response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].ANY);

以下是我回来的而不是像#34; 2016-02-11"因为它位于数据类型" DATE"的DB列中

MjAxNi0wMi0xMQ==

我以为我会在其他所有领域找到一个像我一样的字符串...所以我如何处理MySQL发送回给我将其转换为javascript Date()?

2 个答案:

答案 0 :(得分:1)

看起来像一个base64输出,所以为了让它恢复到正确的日期,只需执行:

atob('MjAxNi0wMi0xMQ=='); // "2016-02-11"

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/atob

alert(atob('MjAxNi0wMi0xMQ=='));

答案 1 :(得分:0)

Shomz hit the nail on the head. the problem is the JS engine I'm using (JustAPIs) does not have the atob or btob functions defined. So I still need to dig through and see WHY these dates are getting base64 encoded.

But in the meantime I found this nice little library that is equivalent to atob and btop. Luckily JustAPIs has a way to add shared libraries, so I just put the following code in there... now have a home made atob/btob.

function btob(a) {
  var c, d, e, f, g, h, i, j, o, b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", k = 0, l = 0, m = "", n = [];
  if (!a) return a;
  do c = a.charCodeAt(k++), d = a.charCodeAt(k++), e = a.charCodeAt(k++), j = c << 16 | d << 8 | e, 
  f = 63 & j >> 18, g = 63 & j >> 12, h = 63 & j >> 6, i = 63 & j, n[l++] = b.charAt(f) + b.charAt(g) + b.charAt(h) + b.charAt(i); while (k < a.length);
  return m = n.join(""), o = a.length % 3, (o ? m.slice(0, o - 3) :m) + "===".slice(o || 3);
}

function atob(a) {
  var b, c, d, e = {}, f = 0, g = 0, h = "", i = String.fromCharCode, j = a.length;
  for (b = 0; 64 > b; b++) e["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(b)] = b;
  for (c = 0; j > c; c++) for (b = e[a.charAt(c)], f = (f << 6) + b, g += 6; g >= 8; ) ((d = 255 & f >>> (g -= 8)) || j - 2 > c) && (h += i(d));
  return h;
}