如何在VBA中将长日期与时间转换为其他格式?

时间:2015-02-16 13:20:56

标签: vba

我的日期数据如下:“2015-02-11T19:41:50-08:00”

我想知道VBA中是否已存在可将上述数据转换为“02/11/2015太平洋标准时间上午11:41”等格式的功能

我尝试使用格式函数播放以下代码,但无法让VBA将格式识别为日期:

testdate = "2015-02-12T22:57:05-08:00"  

newdate = Format(testdate, "mm/dd/yyyy hh/nn/ss AM/PM")  

Debug.Print newdate

输出仍为“2015-02-12T22:57:05-08:00”

感谢您的帮助。

编辑:

我能够通过建议使用mid()函数来解决问题,因为日期是固定格式的。我决定将军事时间保留在最终版本中。

这是我对任何好奇的人的代码:

Function convertDate(orderdate)
'takes the date formatted as 2015-02-06T08:26:00-08:00
'and converts it to mm/dd/yyyy hh/nn/ss UTC format

'2015-02-06T08:26:00-08:00

orderyear = Mid(orderdate, 1, 4)
ordermonth = Mid(orderdate, 6, 2)
orderday = Mid(orderdate, 9, 2)
orderhour = Mid(orderdate, 12, 2)
orderminute = Mid(orderdate, 15, 2)
ordersecond = Mid(orderdate, 18, 2)


newdate = ordermonth & "/" & orderday & "/" & orderyear
newtime = orderhour & ":" & orderminute & ":" & ordersecond

'Debug.Print newdate
convertDate = newdate & " " & newtime & " UTC"

End Function

4 个答案:

答案 0 :(得分:2)

因为您的输入不是真实日期,所以Excel或VBA的日期方法都不适用于它。您最好的选择是将字符串分解为多个部分,单独使用它们,然后再将它们全部加入 - 例如:

testdate = "2015-02-12T22:57:05-08:00"

'// The letter T is redundant, so let's split the string here into an array:
dateArr = Split(testdate, "T")

    '// Part 1 of the array can be easily converted with CDate() and Format()
    dateArr(0) = Format(CDate(dateArr(0)), "mm/dd/yyyy")

    '// Part 2 of the array will need to be broken down further:
    dateArr(1) = Format(TimeValue(Split(dateArr(1), "-")(0)) - _
        TimeSerial(Left(Split(dateArr(1), "-")(1), 2), _
            Right(Split(dateArr(1), "-")(1), 2), 0), "hh:mm:ss")

'// The above line does the following:
'//     1) Split the second part of the array again, using the "-" as the delimiter
'//     2) Convert the first part of this (22:57:05) to a time using TimeValue()
'//     3) Convert the second part (08:00) to hours & minutes using TimeSerial()
'//     4) Minus the latter from the former (which can only be done if both are a valid time)
'//     5) Wrap all that into a Format() method to show "hh:mm:ss" instead of a Double.


    '// Join the two parts back together and add "PST" on the end.
    newdate = Join(dateArr, " ") & " PST"

Debug.Print newdate

'// Output will display "02/12/2015 14:57:05 PST"

N.B。 我选择不包括" AM"或" PM"因为你的时间是24小时格式,所以我没有看到相关性...

答案 1 :(得分:1)

由于“T”而没有转换,并且因为最后加上时间范围。您可以抛弃“T”并截断尾随范围,它将转换。

Public Sub Example()
    Const testValue As String = "2015-02-12T22:57:05-08:00"
    Dim dateValue As Date
    Dim stringValue As String
    Dim subVal As Date
    Dim hyphenPos As Long
    stringValue = testValue
    Mid(stringValue, 11&, 1&) = " "
    hyphenPos = InStrRev(stringValue, "-")
    subVal = Mid$(stringValue, hyphenPos + 1&)
    dateValue = CDate(Left$(stringValue, hyphenPos - 1&)) - subVal
End Sub

答案 2 :(得分:0)

一些想法:

  1. 您拥有2015-02-12T22:57:05-08:00的样本日期不是真实日期(我认为)
  2. 我认为以下内容会为您提供最接近您要查找内容的格式(您需要定义范围。Range.NumberFormat = "[$-409]h:mm:ss AM/PM"

答案 3 :(得分:0)

您最好的选择是将“PST”与根据您的需要格式化的日期数据类型相结合。

Sub DebugPrintDate()
Dim testdate As Date: testdate = Now
newdate = Format(testdate, "mmm/dd/yyyy hh:mm AM/PM") & " PST"
Debug.Print newdate
End Sub

输出继电器:

enter image description here

别介意“févr”。我的系统区域是法国。

如果要定义特定日期,请确保将日期换成两个#s。

示例:

  Dim someDateAndTime As Date = #8/13/2002 12:14 PM#