【问题标题】:How to convert a string into date如何将字符串转换为日期
【发布时间】:2012-09-19 13:14:43
【问题描述】:

我正在尝试将字符串转换为日期

a = Datumo: 16.09.2012.

d = Mid(a, 9, 2) ' 16 - should be the day
m = Mid(a, 12, 2) ' 09 - month
y = Mid(a, 15, 4) ' 2012 - year

Dim dt As Date 
dt = DateValue(d, m, y) ' Error line 
dt = DateAdd("d", 1, dt) ' should be 17.09.2012.

请问我该怎么做?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    DateValue 的用法不正确,替换

    dt = DateValue(d, m, y) ' Error line 
    dt = DateAdd("d", 1, dt) ' should be 17.09.2012.
    

    dt = DateSerial(y, m, d) 
    dt = DateAdd("d", 1, dt)
    

    dt = 1 + DateAdd("d", 1, dt)
    

    【讨论】:

    • DateSerial 或 DateValue 的问题在于 VBA 将无效日期转换为有效日期。在某些情况下可以,但如果您想将字符串转换为日期,通常不会。例如尝试输入dt = DateSerial(2012, 14, 42)
    • @ooo,非常有用的帖子。谢谢。
    【解决方案2】:

    看看DatePart函数;)

    来自 excel-帮助:

    Dim Datum1 As Date    ' Variablen deklarieren.
    Dim Msg
    Datum1 = InputBox("Geben Sie ein Datum ein:")
    Msg = "Quartal: " & DatePart("q", Datum1)
    MsgBox Msg
    

    还有一些特定的功能可以检索有效日期的部分内容:

    Day()
    Month()
    Year()
    

    来自 excel-help 的示例:

    Dim Datum1, Tag1
    Datum1 = #12. Februar 1969#    ' Datum zuweisen.
    Tag1 = Day(Datum1)    ' Tag1 enthält 12.
    

    【讨论】:

      【解决方案3】:

      VBA/Excel 经常会尝试确定日期并弄错。

      例如

      dt = DateSerial(2012, 14, 42) 'returns #14/03/2013#
      
      dtReal = IsDate("09 / 14 / 2012") 'returns TRUE when we really wanted to test dd/mm/yyyy - there's no 14th month.
      

      我经常使用下面的代码,这是一种较长但万无一失的方法来确定字符串日期是否真实并将其作为日期返回。

      Option Explicit
      
          Sub test()
          Dim testDate As Boolean
          Dim dt As Date
      
          testDate = IsTextDate("05/07/2012", dt, "dd/mm/yyyy") 'Could pass "mm/dd/yyyy" instead
          If testDate = True Then
              Debug.Print "Valid Date " & Format(dt, "dd-mmm-yyyy")
          Else
              Debug.Print "Invalid Date"
          End If
          End Sub
      
          Function IsTextDate(ByVal str As String, ByRef dt As Date, Optional dtFormat As String = "dd/mm/yyyy") As Boolean
          Dim D As Date
          Dim day As Integer
          Dim month As Integer
          Dim year As Long
          Dim daysinFeb As Integer
          Dim dtArray() As String
      
          'Test input string mask matches date format
          If Not (str Like "##/##/####") Then
              'Debug.Print "Invalid Date"
              Exit Function
          Else
              'split string on delimiter and extract correct 'dd' and 'mm' based on UK/US format
              dtArray = Split(str, "/")
              year = CInt(dtArray(2))
              If dtFormat = "dd/mm/yyyy" Then
                  day = CInt(dtArray(0))
                  month = CInt(dtArray(1))
              Else
                  day = CInt(dtArray(1))
                  month = CInt(dtArray(0))
              End If
      
              'determine days in Feb for year given.
              If IsDate("2/29/" & year) Then
                  daysinFeb = 29
              Else
                  daysinFeb = 28
              End If
      
              'determine if date is valid
              Select Case month
                  Case 9, 4, 6, 11
                      '30 days
                      If day <= 30 Then IsTextDate = True
      
                  Case 1, 3, 5, 7, 8, 10, 12
                      '31 days
                      If day <= 31 Then IsTextDate = True
      
                  Case 2
                      'either 28 or 19 days
                      If day <= daysinFeb Then IsTextDate = True
      
              End Select
              If IsTextDate Then dt = DateSerial(year, month, day)
      
          End If
      
          End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        • 1970-01-01
        • 2023-02-06
        • 2016-11-10
        • 2015-04-27
        • 2011-11-28
        相关资源
        最近更新 更多