将DateTime格式化为DateTime以毫秒为单位

时间:2016-11-14 19:32:37

标签: csv datetime vbscript timestamp db2

我将数据从数据库中拉入记录集,然后转换为数组,然后写入CSV。

在数据库中,所有日期值都以此格式存储为时间戳。

2016-05-04 08:00:00.000000

但是当我写入CSV文件时,时间戳不包括毫秒。

任何人都知道如何保留毫秒? 记录集中的数据是否包含毫秒数?

On Error Resume Next
Dim sPassword
Dim sUserID
Dim sDefaultLib
Dim sSystem
Dim cs
Dim rc
Dim objIEDebugWindow

sDefaultLib =  *library*
sUserID = *userid*
sPassword = *password*
sSystem = *system*
cs = *connectionString*

Set con = CreateObject("ADODB.Connection")
Set data = CreateObject("ADODB.Recordset")
con.Open cs, sUserID, sPassword
rc = con.State

If (rc = 1) Then
  strQuery = "SELECT * FROM Library.Table FETCH FIRST 15 ROWS ONLY FOR READ ONLY WITH UR"
  data.CursorLocation = adUseClient
  data.Open strQuery, con
  Set filsSysObj = CreateObject("Scripting.FileSystemObject")
  Dim theYear
  Dim theMonth
  Dim theDay
  Dim mDate
  mDate = Date()
  theYear = DatePart("yyyy", mDate)
  theMonth = Right(String(2, "0") & DatePart("m", mDate), 2)
  theDate = Right(String(2, "0") & DatePart("d", mDate), 2)
  mDate = theYear & theMonth & theDate
  Set csvFile = filsSysObj.OpenTextFile("C:\SampleFile_" & mDate & ".csv", 8, True)

  columnCount = data.Fields.Count

  Set i = 0
  For Each field In data.Fields
    i= i + 1
    If (i <> columnCount) Then
      csvFile.Write Chr(34) & field.Name & Chr(34) & ","
    Else
      csvFile.Write Chr(34) & field.Name & Chr(34)
    End If
  Next
  csvFile.Write vbNewLine
End If

rowCount = data.RecordCount
row = 0

Dim row
Dim column
Dim resultsArray
Dim dateArray
resultsArray = data.GetRows

debug "hi"

i = 0
Do Until i>5
  MsgBox(i)
  i = i + 1
  'debug "in"
  'Dim value
  'Dim dArray()
  'debug "in"
  'value = Chr(34) & CStr(data.Fields(17).Value) & Chr(34) & ","

  'dArray = additem(dArray, value)
  'data.MoveNext
  'dateArray = dArray
Loop

debug "out"

For row = 0 To UBound(resultsArray, 2)
  For column = 0 To UBound(resultsArray, 1)
    If row = UBound(resultsArray, 2) And column = UBound(resultsArray, 1) Then
      csvFile.Write Chr(34) & resultsArray(column, row) & Chr(34)
    Else
      If column = 0 Then
        csvFile.Write Chr(34) & formatDate(resultsArray(column, row)) & Chr(34) & ","
      ElseIf column = 19 Then
        csvFile.Write Chr(34) & FormatDateTime(resultsArray(column, row),4) & Chr(34) & ","
      ElseIf column = 18 Then
        csvFile.Write Chr(34) & formatDate(resultsArray(column, row)) & Chr(34) & ","
      'ElseIf column = 17 Then
        'csvFile.Write Chr(34) & formatDate(resultsArray(column, row)) & Chr(34) & ","
      Else
        csvFile.Write Chr(34) & resultsArray(column, row) & Chr(34) & ","
      End If
    End If
  Next
  csvFile.Write vbNewLine
Next

csvFile.close

'----------------------Helper Functions are below-----------------------------
Sub Debug(myText)
  'Dim objIEDebugWindow must be defined globally
  'Call like this "Debug variableName"
  'Uncomment the next line to turn off debugging
  'Exit Sub

  If Not IsObject(objIEDebugWindow) Then
    Set objIEDebugWindow = CreateObject("InternetExplorer.Application")
    objIEDebugWindow.Navigate "about:blank"
    objIEDebugWindow.Visible = True
    objIEDebugWindow.ToolBar = False
    objIEDebugWindow.Width   = 200
    objIEDebugWindow.Height  = 300
    objIEDebugWindow.Left    = 10
    objIEDebugWindow.Top     = 10
    Do While objIEDebugWindow.Busy
      WScript.Sleep 100
    Loop
    objIEDebugWindow.Document.Title = "IE Debug Window"
    objIEDebugWindow.Document.Body.InnerHTML = "<b>" & Now & "</b></br>"
  End If

  objIEDebugWindow.Document.Body.InnerHTML = objIEDebugWindow.Document.Body.InnerHTML & myText & "<br>" & vbCrLf
End Sub

Function formatDate(sDate)
  Dim theYear
  Dim theMonth
  Dim theDay
  Dim formattedDate

  theYear = Year(sDate)
  theMonth =  Right(String(2,"0") & DatePart("m", sDate),2)
  theDay = Right(String(2,"0") & DatePart("d", sDate),2)
  formattedDate = theYear & "-" & theMonth & "-" & theDate
  formatDate = formattedDate
End Function

我遇到问题的唯一字段是记录集的字段17。 它是来自DB2数据库的时间戳数据类型。

1 个答案:

答案 0 :(得分:0)

问题是格式是DB2数据库中的时间戳。当我进入记录集时,它会丢失毫秒。我的解决方案是修改查询以添加一个额外的行,该行仅在几毫秒内拉入,然后再将其连接回日期。请看下面。感谢大家的帮助。

public class EditProducts extends  JInternalFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;


public EditProducts(){

    this.add(new JLabel("hello"));
}