使程式調用伺服器時間

來源:互聯網
上載者:User

        在做機房收費系統的時候,沒有想到多使用者操作的問題。在調試的時候,賈琳提出了此問題。由於此系統是在上下機的時候讀取時間,然後做差。那麼在多使用者操作時,如果這些使用者的時間不一致,那麼就會發生錯誤。所以,在使用此系統時,全部都調用伺服器時間,這樣就可以避免此問題了。下面是調用伺服器日期和時間的函數。

Public Function getDate() As String    Dim strCn As String, strRs As String         Dim objRs As Recordset, objCn As Connection        strCn = "Provider=SQLOLEDB;Data Source=192.168.24.61;Initial CataLog=charge_my;UID=sa;PWD=123456"    Set objCn = New Connection        objCn.Open strCn        strRs = "select replace(convert(varchar(10),getdate(),120),N'-0','-')"        Set objRs = New Recordset    objRs.Open Trim$(strRs), objCn, adOpenKeyset, adLockOptimistic        getDate = objRs.Fields(0)    End FunctionPublic Function getTime() As String    Dim strCn As String, strRs As String    Dim objCn As Connection, objRs As Recordset        strCn = "Provider=SQLOLEDB;Data Source=192.168.24.61;Initial CataLog=charge_my;UID=sa;PWD=123456"    Set objCn = New Connection        objCn.Open strCn        strRs = "select CONVERT(varchar, getdate(), 108)"        Set objRs = New Recordset        objRs.Open Trim$(strRs), objCn, adOpenKeyset, adLockOptimistic        getTime = objRs.Fields(0)End Function  

       主要是用了convert這個函數來擷取想要的時間格式。由於我日期和時間都設定的是字元型,所以如果格式不一樣的話,比較會出錯。而convert這個函數幫我解決了時間格式問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.