获取系统的日期分隔符
Get system's date delimiter
我需要将各种 Excel 文件导入到 Access 数据库中。有两个日期列使用所有可能的格式,例如 01.05.2020
或 01,05,2020
或 01/05/2020
,具体取决于它们的创建方式。有时它是有效日期,有时它只是一个字符串。
Access 应用程序也会 运行 在不同的系统上,因此它们可能使用不同的分隔符。这意味着我不能只用 /
替换所有 .
或 ,
。我将创建一个替换函数,该函数可以为 运行 所在的任何系统规范化字符串,但我无法弄清楚如何获取 当前系统的日期分隔符 .
VBA可以给我那个信息吗?
使用 XlApplicationInternational 枚举和 Application.International
属性 检索此配置。
这个 returns /
在我的系统上:
Excel.Application.International(xlApplicationInternational.xlDateSeparator)
如果您在 Access 主机中,您将希望避免使用全局 Excel.Application
对象,并使用您拥有的实例,并且您可以 Quit
完成:
Dim app As Excel.Application
Set app = New Excel.Application
Debug.Print app.International(xlApplicationInternational.xlDateSeparator)
'...
app.Quit
真正简单快捷的方法就是让VBA把字符“/”做成格式指定日期分隔符:
' Get localised date separator.
DateSeparator = Format(Date, "/")
' Denmark -> "-"
' Germany -> "."
' US -> "/"
为什么这有效:
(/) - Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character used as the date separator in formatted output is determined by your system settings.
我需要将各种 Excel 文件导入到 Access 数据库中。有两个日期列使用所有可能的格式,例如 01.05.2020
或 01,05,2020
或 01/05/2020
,具体取决于它们的创建方式。有时它是有效日期,有时它只是一个字符串。
Access 应用程序也会 运行 在不同的系统上,因此它们可能使用不同的分隔符。这意味着我不能只用 /
替换所有 .
或 ,
。我将创建一个替换函数,该函数可以为 运行 所在的任何系统规范化字符串,但我无法弄清楚如何获取 当前系统的日期分隔符 .
VBA可以给我那个信息吗?
使用 XlApplicationInternational 枚举和 Application.International
属性 检索此配置。
这个 returns /
在我的系统上:
Excel.Application.International(xlApplicationInternational.xlDateSeparator)
如果您在 Access 主机中,您将希望避免使用全局 Excel.Application
对象,并使用您拥有的实例,并且您可以 Quit
完成:
Dim app As Excel.Application
Set app = New Excel.Application
Debug.Print app.International(xlApplicationInternational.xlDateSeparator)
'...
app.Quit
真正简单快捷的方法就是让VBA把字符“/”做成格式指定日期分隔符:
' Get localised date separator.
DateSeparator = Format(Date, "/")
' Denmark -> "-"
' Germany -> "."
' US -> "/"
为什么这有效:
(/) - Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character used as the date separator in formatted output is determined by your system settings.