Table of contents

将日期设置为与区域设置无关的美国日期格式Format Dates as U.S. Dates Regardless of Regional Settings

office 365 dev account|上次更新日期: 2017/9/25
1 参与者

当使用 Visual Basic for Applications (VBA) 可以将日期连接到 SQL 字符串时,必须使用标准的美国日期格式,无论您在控制面板区域设置工具中设置的区域。本主题包含将任何日期转换为美国日期格式的自定义函数。When you use Visual Basic for Applications (VBA) to concatenate dates into an SQL string, you must use a standard U.S. date format, regardless of the region that you have set in the Regional Settings tool in Control Panel. This topic contains a custom function that will convert any date into a U.S. date format.

Function MakeUSDate(DateIn As Variant) As String 

 ' Do nothing if the value is not a date. 
 If Not IsDate(DateIn) Then Exit Function 

 ' Convert the date to a U.S. Date format. 
 MakeUSDate = "#" &; Month(DateIn) &; "/" &; Day(DateIn) &; "/" &; Year(DateIn) &; "#" 
End Function
© 2018 Microsoft