英文和德文的通用日期格式公式

问题描述 投票:1回答:1

我在Excel中有一个公式以指定格式设置日期格式(=text(date,"dd-mm-yyyy"),该文件也用在瑞士,其系统日期设置用德语(swiss)。因此无法正确显示它。如何在公式中执行此操作?

我不想格式化单元格。我在公式中需要它。

=text(date,"dd-mm-yyyy")

该文件将同时被两个用户(英语和德语)使用。为此需要一个通用公式。以及如何在vba中执行相同操作我用过

format$(date,"dd-mm-yyyy")

对于英语。

excel date format
1个回答
0
投票

'从http://www.vbforums.com/showthread.php?33984-How-to-GET-the-system-date-format-from-regional-settings改编的代码

选项显式

Private Declare PtrSafe Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Const LOCALE_SLONGDATE = &H20 ' long date format string

Function LocaleString()
    Dim strLocale As String
    Dim lngRet As Long
    Dim strMsg As String

    'Get short date format
    strLocale = Space(255)
    lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strLocale, Len(strLocale))
    strLocale = Left(strLocale, lngRet - 1)
    LocaleString = strLocale
End Function

Function CDateLocale(d As Date) As String
  CDateLocale = Format(d, LocaleString())

End Function

在模块中,您可以使用= CDateLocale(A1)将A1中的日期转换为本地shortdate格式,但以文本而不是日期的形式。

© www.soinside.com 2019 - 2024. All rights reserved.