vba 如何更改格式化日期的语言环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16934582/
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-11 21:27:56  来源:igfitidea点击:

How to change the locale of a formatted date?

vbalocaledate-format

提问by mafodope

I want to retrieve today's date in a specific format with English month name.

我想用英文月份名称以特定格式检索今天的日期。

I'm using Format(DateValue(Now), "dd-mmm-yyyy"), which gives me 05-cze-2013, which is in Polish. What I want to get is 05-Jan-2013.

我正在使用 Format(DateValue(Now), "dd-mmm-yyyy"), 这给了我 05-cze-2013, 这是波兰语。我想得到的是 05-Jan-2013.

I am only interested in a VBA solution. Please also provide a way to set the locale back to the original, also using VBA.

我只对 VBA 解决方案感兴趣。还请提供一种将语言环境设置回原始语言的方法,也使用 VBA。

回答by

It's not very difficult...

这不是很困难...

Sub VBA_Dates_Format()
    Cells.Clear
    Range("A1").Value = Now
    Range("A2").Value = Now
    ' Polish
    Range("A1").NumberFormat = "[$-415]d mmm yy;@"
    ' English UK
    Range("A2").NumberFormat = "[$-809]d mmm yy;@"
End Sub

I have achieved this by recording and modifying a macro to fit the criteria.

我通过记录和修改宏以符合标准来实现这一点。



Further reading available here进一步阅读在这里

回答by b0rek

It's not clean VBA but it's a way to get localized formatted date in string. Something that Format function can't do.

它不是干净的 VBA,但它是一种在字符串中获取本地化格式化日期的方法。Format 功能不能做的事情。

With Range("$A")
    .NumberFormat = "[$-809]dd mmmm"
    .FormulaR1C1 = "=NOW()"
    tmp = .Text
    .NumberFormat = "@"
    .Value = CStr(tmp)
End With

More info about localized formatting i great answer https://stackoverflow.com/a/899290from Ian Boyd

有关本地化格式的更多信息我很好回答https://stackoverflow.com/a/899290来自 Ian Boyd