关于日期


本示例可参考示例文件"CaseStudy\WebViewer\调用Foxtable对象.Table"的窗口"使用SQLCommand"和"使用Var变量"。

前端js读取Foxtable中的日期数据时,得到的不是日期值,而是一个数值,这个数值表示从1899年12月30日午夜到该日期之间的天数。

而js的起始日期是1970年1月1日,二者之间相差25569天。

所以js从Foxtable读取日期值后,要进行转换,转换函数为:

function oaToDate(oaDate) {
    const baseDays = 25569;
    return new Date((oaDate - baseDays) * 86400 * 1000);
}

示例

下面是"使用Var变量"这个窗口的AfterLoad事件代码,演示了一个日期型变量的创建、修改和读取:

'''Async
Dim
wv As WebViewer = e.Form.Controls("WebViewer1").WebViewer
Await
wv.EnsureCoreWebView2Async(Nothing)
Dim ft As New FoxLib(wv) '创建Foxtable
wv.CoreWebView2.AddHostObjectToScript("ft", ft)  '注入Foxtable
Dim
html As String = <![CDATA[
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>
使用Var变量</title>
    <style>
        button{padding:12px 40px;font-size:18px;margin:30px;background:#0078d7;color:#fff;border:none;border-radius:6px;cursor:pointer;}
        button:hover{background:#005a9e;}
        h2{color:#333;margin:20px;}
    </style>
</head>
<body>
    <h2>
使用Var变量</h2>
    <button onclick="addVars()">
添加Var变量</button>
    <button onclick="modifyVars()">
修改Var变量</button>
    <button onclick="showVars()">
显示Var变量</button>
    <script>
        const ft = window.chrome.webview.hostObjects.sync.ft;
       
        //
极简工具函数:将OADate数值转换为JS Date对象
        function oaToDate(oaDate) {
            const baseDays = 25569;
            return new Date((oaDate - baseDays) * 86400 * 1000);
        }
 
        function addVars() {
            ft.Vars.Add("Mark1", "Boolean", false);
            ft.Vars.Add("
起始日期", "Date", new Date(2007, 11, 31));
        }
 
        function modifyVars() {
            ft.Vars["Mark1"] = true;
            ft.Vars["
起始日期"] = new Date(); //今天日期
        } 

        function showVars() {
            const mark = ft.Vars["Mark1"];
            const readableDate = oaToDate(ft.Vars["
起始日期"]).toLocaleDateString();
            alert(`Mark1
值:${mark}\n起始日期值:${readableDate}`);
        }
    </script>
</body>
</html>
]]>
.Value
wv.NavigateToString(html)

提示:不管是Var变量,全局变量还是日期列,都要进行上述转换。


本页地址:http://www.foxtable.com/webhelp/topics/6342.htm