vba 如何使用excel vba检查操作系统系统日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22739362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:34:24  来源:igfitidea点击:

how to check OS system date format using excel vba

excelvbaexcel-vba

提问by Leon

I am using excel 2007, ms visual basic 6.0. I required to check the window os date format (e.g, whether is it using d/m/yyyy or m/d/yyyy), in order to using the following code.

我使用的是 excel 2007,ms visual basic 6.0。我需要检查window os 日期格式(例如,它是使用d/m/yyyy 还是m/d/yyyy),以便使用以下代码。

Dim lastdateofmonth As Date
Dim lastwhichday As String
slastdayofmonth = "31"
'if the OS system is using m/d/yyyy then use this
lastdateofmonth = (sTxtMMM + "/" + slastdayofmonth + "/" + TxtYYYY)
lastwhichday = Weekday(lastdateofmonth)
'if th OS system is using d/m/yyyy then use this
lastdateofmonth = (slastdayofmonth+ "/" + sTxtMMM  + "/" + TxtYYYY)

anyone can help? Thanks in advance

任何人都可以帮忙吗?提前致谢

回答by Leon

hmm... i found a better way

嗯……我找到了更好的方法

'========== Check OS Date format========
Dim OSDateFormatType As Integer
'  0 = month-day-year;   1 = day-month-year;   2 = year-month-day
If Application.International(xlDateOrder) = 0 Then
    OSDateFormatType = 0
ElseIf Application.International(xlDateOrder) = 1 Then
    OSDateFormatType = 1
ElseIf Application.International(xlDateOrder) = 2 Then
    OSDateFormatType = 2
End If

But this only work for excel.

但这仅适用于excel。

回答by Fox

The best way:

最好的方法:

if Application.International(xlMDY) then ...

True - month, day, year False - day, month, year

True - 月、日、年 False - 日、月、年

https://msdn.microsoft.com/en-us/library/office/ff840213%28v=office.15%29.aspx

https://msdn.microsoft.com/en-us/library/office/ff840213%28v=office.15%29.aspx