短日期的数字格式代码是什么?

时间:2022-07-23 23:05:53

I know that I can write the following to format a number as a date with the appearance yyyy-mm-dd:

我知道我可以编写以下内容来将数字格式化为具有yyyy-mm-dd外观的日期:

Range("A1").NumberFormat = "yyyy-mm-dd"

I know that I can also write:

我知道我也可以写:

Range("A1").NumberFormat = "m/d/yyyy"

and all kinds of things.

和各种各样的事情。

But is there a way I can set the number format to become a general short date and leave it up to the user's system to decide the exact appearance?

但有没有办法可以将数字格式设置为一般的短日期,并将其留给用户的系统来决定确切的外观?

I'm looking for an expression like:

我正在寻找一个像这样的表达式:

Range("A1").NumberFormat = "Short Date" '(<-This does not work)

2 个解决方案

#1


7  

It is a bit tricky. If you have a look at a cell's properties, format tab, category Date, you will see some formats beginning with an asterisk (*), those respond to the changes of the user locale. Try setting your cell to the first element, short date, then get its numberformat value in the immediate window with ?activecell.NumberFormat. You'll see that it is "m/d/yyyy", so your snippet does the job well. Just try changing your locale setting in control panel, the format of the cell will change accordingly.

这有点棘手。如果您查看单元格的属性,格式选项卡,类别日期,您将看到一些以星号(*)开头的格式,这些格式会响应用户区域设置的更改。尝试将单元格设置为第一个元素,短日期,然后使用?activecell.NumberFormat在即时窗口中获取其numberformat值。你会发现它是“m / d / yyyy”,所以你的代码片段可以很好地完成工作。只需尝试在控制面板中更改区域设置,单元格的格式就会相应更改。

#2


-3  

Please try this

请试试这个

 Format(Range("A1"), "Short Date")

It will return the system short date value.

它将返回系统的短日期值。

#1


7  

It is a bit tricky. If you have a look at a cell's properties, format tab, category Date, you will see some formats beginning with an asterisk (*), those respond to the changes of the user locale. Try setting your cell to the first element, short date, then get its numberformat value in the immediate window with ?activecell.NumberFormat. You'll see that it is "m/d/yyyy", so your snippet does the job well. Just try changing your locale setting in control panel, the format of the cell will change accordingly.

这有点棘手。如果您查看单元格的属性,格式选项卡,类别日期,您将看到一些以星号(*)开头的格式,这些格式会响应用户区域设置的更改。尝试将单元格设置为第一个元素,短日期,然后使用?activecell.NumberFormat在即时窗口中获取其numberformat值。你会发现它是“m / d / yyyy”,所以你的代码片段可以很好地完成工作。只需尝试在控制面板中更改区域设置,单元格的格式就会相应更改。

#2


-3  

Please try this

请试试这个

 Format(Range("A1"), "Short Date")

It will return the system short date value.

它将返回系统的短日期值。