简体   繁体   English

在Excel VBA中更改日期格式

[英]Change Date format in Excel VBA

I have the Date in the log file. 我在日志文件中有日期。 The Date in the log file looks something like this : 日志文件中的日期看起来像这样:

Mon May 14 21:31:00 EDT 2012 2012年5月14日星期一21:31:00

I would like to convert this into the format "dd-MM-yyyy hh:mm:ss" 我想将其转换为“ dd-MM-yyyy hh:mm:ss”格式

Dim DateVal1 As String 
Dim Date1 As Date

DateVal1 = "Mon May 14 21:31:00 EDT 2012"

Date1 = Format(DateVal,  "ddd mmm dd  hh:mm:ss EDT yyyy")
Date1 = Format(Date1 , "dd-MM-yyyy hh:mm:ss")

The following code is not converting into the format that I was expecting. 以下代码未转换为我期望的格式。 Any Idea, If I am missing something here. 任何想法,如果我在这里错过了一些东西。

Thanks 谢谢

You might want to consider extracting datetime components from your custom datetime string first. 您可能要考虑首先从自定义日期时间字符串中提取日期时间组件。 For example, you can get day, month, year, etc. You could probably utilize string manipulation functions, like Mid , Replace and so on. 例如,您可以获取日,月,年等。您可能可以利用字符串处理功能,例如MidReplace等。

If your custom string is consistent, then you can create your own function that does string manipulations and outputs the datetime value. 如果您的自定义字符串是一致的,则可以创建自己的函数来执行字符串操作并输出datetime值。

Just a thought here. 这里只是一个想法。 Hope it helps. 希望能帮助到你。

If you are running English version of Office (and have English operating system) this function should give your results which you need: 如果您正在运行英文版的Office(并具有英文操作系统),则此功能应提供所需的结果:

Mon May 14 21:31:00 EDT 2012 >> 2012-05-14 21:31:00 2012年5月14日星期一5:14 EDT >> 2012-05-14 21:31:00

Function DateConversion(DateVal1 As String)

    Dim DateParts As Variant
    DateParts = Split(DateVal1, " ")

    Dim i
    For i = 1 To 12
        If InStr(1, Left(MonthName(i), 3), DateParts(1), vbTextCompare) > 0 Then Exit For
    Next i

    DateConversion = CDate(DateSerial(DateParts(UBound(DateParts)), i, DateParts(2)) & " " & DateParts(3))

End Function

However, if you are using any other language it could require some additional changes referring to month names (MonthName function returns month names in your system language). 但是,如果您使用任何其他语言,则可能需要一些其他更改来引用月份名称(MonthName函数以您的系统语言返回月份名称)。

EDIT: Solution for situation of different languages month names 编辑:不同语言月份名称情况的解决方案

In this situation we need to create additional array with month names to be able to compare part of the original data string. 在这种情况下,我们需要使用月份名称创建其他数组,以便能够比较部分原始数据字符串。

Function DateConversionEng(DateVal1 As String)

    Dim DateParts As Variant
        DateParts = Split(DateVal1, " ")
    Dim Months As Variant
        Months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "Octover", "November", "December")

    Dim i
    For i = 1 To 12
        If InStr(1, Months(i), DateParts(1), vbTextCompare) > 0 Then Exit For
    Next i

    DateConversionEng = CDate(DateSerial(DateParts(UBound(DateParts)), i + 1, DateParts(2)) & " " & DateParts(3))

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM