It seems that every week or so someone posts a question about dates being converted (corrupted?) to American format. Like many others, I have attempted to help but the problem is elusive. I now wonder if I have discovered the cause.
似乎每个星期左右有人发布一个关于日期被转换(损坏?)到美国格式的问题。像许多其他人一样,我试图帮助但问题难以捉摸。我现在想知道我是否发现了原因。
I am working on an application in which I need to extract data from an Excel worksheet and output it as strings formatted to match the value the Excel user can see. So if the value is “1” formatted to display as “1.00” then I want the string to be “1.00”.
我正在开发一个应用程序,我需要从Excel工作表中提取数据并将其输出为格式化的字符串,以匹配Excel用户可以看到的值。因此,如果值为“1”格式化为显示为“1.00”,那么我希望字符串为“1.00”。
I achieve this effect by testing the cell value to be a number, date or time. If it is, I retrieve the number format and use it to format the cell value so:
我通过将单元格值测试为数字,日期或时间来实现此效果。如果是,我检索数字格式并使用它来格式化单元格值,以便:
With .Cells(Row, Column)
Output string = Format(.Value, .NumberFormat)
End With
In most cases this gives me exactly the output I require. However, sometimes I get American dates and times when the source is formatted as a UK date or time.
在大多数情况下,这给了我正确的输出。但是,有时我会将源格式化为英国日期或时间的美国日期和时间。
After much experimentation with Excel 2003 and Excel 2007, I have discovered the cause. (I do not have access to Excel 2010 but from questions I deduce it has the same problem.) This question is in part intended to reveal this problem to the world because I can discover nothing on the internet to indicate that anyone else has noticed it. (No doubt someone will reply that they googled “xyz” and got the answer immediately.) However, the main purpose of this question is to seek suggestions for obtaining the result I need in all situations.
经过Excel 2003和Excel 2007的大量实验,我发现了原因。 (我无法访问Excel 2010,但是从我推断的问题来看,它有同样的问题。)这个问题部分旨在向全世界揭示这个问题,因为我无法在互联网上发现任何其他人已经注意到它。 (毫无疑问,有人会回复他们用谷歌搜索“xyz”并立即得到答案。)但是,这个问题的主要目的是寻求建议,以获得我在所有情况下所需的结果。
Typically I enter dates as, for example, “23mar12”. Excel recognises this as a date and formats it as “23-Mar-12”. I can select Format Cells
and enter or select a custom format or select one of the date formats so I can have any format I can imagine wanting including non-English names for days and months.
通常我输入日期,例如“23mar12”。 Excel将此识别为日期并将其格式化为“23-Mar-12”。我可以选择格式单元格并输入或选择自定义格式或选择其中一种日期格式,这样我就可以拥有任何我想象的格式,包括日期和月份的非英文名称。
However, in one case the format I select is not the format that is recorded: Custom format “dd/mm/yyyy” is recorded as Date format “* 14/03/2001”. This is not obviously a problem until further down the line.
但是,在一种情况下,我选择的格式不是记录的格式:自定义格式“dd / mm / yyyy”被记录为日期格式“* 14/03/2001”。直到下一步,这显然不是问题。
I created a column of dates and times and formatted each with a different custom or standard format. I wrote a macro to extract the NumberFormat for each of these dates and times and write it as a string to an adjacent column. I also formatted the value using the number format and wrote that string to a third column.
我创建了一列日期和时间,并使用不同的自定义或标准格式对每个格式进行格式化。我写了一个宏来为这些日期和时间中的每一个提取NumberFormat,并将其作为字符串写入相邻列。我还使用数字格式格式化了值,并将该字符串写入第三列。
In a number of cases the format selected and recorded by Excel was not the format returned by NumberFormat:
在许多情况下,Excel选择和记录的格式不是NumberFormat返回的格式:
Excel format NumberFormat
Date: * 14/03/2001 m/d/yyyy
Date: * 14 March 2001 [$-F800]dddd, mmmm dd, yyyy
Date: 14/03/2001 dd/mm/yyyy;@
Date: 14/03/01 dd/mm/yy;@
Date: 14/3/01 d/m/yy;@
Date: 14.3.01 d.m.yy;@
Date: 2001-03-14 yyyy-mm-dd;@
Date: 14 March 2001 (1) [$-809]dd mmmm yyyy;@
Date: 14 March 2001 (2) [$-809]d mmmm yyyy;@
Custom: hh:mm:ss h:mm:ss
Time: * 13:30:55 [$-F400]h:mm:ss AM/PM
Time: 13:30:55 (1) hh:mm:ss;@
Time: 13:30:55 (2) h:mm:ss;@
Time: 01:30:55 PM [$-409]hh:mm:ss AM/PM;@
Time: 1:30:55 PM [$-409]h:mm:ss AM/PM;@
The values (1) and (2) in the Excel format column were added by me to indicate that there are two apparently identical formats. As can be seen from the NumberFormat column, in each case the second version suppresses a leading zero.
我添加了Excel格式列中的值(1)和(2),表示有两种明显相同的格式。从NumberFormat列可以看出,在每种情况下,第二个版本都会抑制前导零。
Most changes have no important effect. “[$-F800]” and so on are apparently dummy values with no effect. Apparently you can replace “F800” with an Microsoft country code to have the names of days and months translated to the language of that country.
大多数变化都没有重要影响。 “[$ -F800]”等显然是虚拟值而没有效果。显然,您可以将“F800”替换为Microsoft国家/地区代码,以将日期和月份的名称翻译为该国家/地区的语言。
However, the three standard formats that Microsoft marks with an asterisk are changed unacceptably. The dates are changed from little endian to middle endian; the time is changed from 24 hour to 12 hour and the day of the week has been added to “* 14 March 2001”.
但是,Microsoft标记为星号的三种标准格式无法接受。日期从小端到中端改变;时间从24小时变为12小时,并将星期几添加到“2001年3月14日”。
The asterisk against the dates, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), date formats that you apply do not switch date orders with the operating system.” The asterisk against the time, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), time formats that you apply do not switch time orders with the operating system.”
对日期的星号引用注释:“除了类型列表(数字选项卡,格式单元格对话框)中带有星号()的项目外,您应用的日期格式不会切换操作系统的日期顺序。”星号与时间相对应,引用注释:“除了类型列表(数字选项卡,格式单元格对话框)中带有星号()的项目外,您应用的时间格式不会随操作系统切换时间顺序。”
If I have to, I can warn my users that standard date and time formats may not give the result desired. However, if they want the popular format “dd/mm/yyyy”, they cannot have it. “dd-mm-yyyy”, for example, is OK but custom format “dd/mm/yyyy” becomes date format “* 14/03/2001” becomes “m/d/yyyy”.
如果必须,我可以警告我的用户标准日期和时间格式可能无法提供所需的结果。但是,如果他们想要流行的格式“dd / mm / yyyy”,他们就无法拥有它。例如,“dd-mm-yyyy”可以,但是自定义格式“dd / mm / yyyy”变为日期格式“* 14/03/2001”变为“m / d / yyyy”。
Returning to my opening point: is this strange handling of one particular date format the reason so many people claim their dates are sometimes being converted to American format and is this why the problem is so elusive? I have come across this type of problem elsewhere of one group of Microsoft programmers not knowing what another group are doing. Is this why some functions always work and other sometimes don’t? Some Microsoft programmers know where to look for the correct format and others don’t?
回到我的开篇点:这是一个奇怪的处理一个特定的日期格式的原因,这么多人声称他们的日期有时被转换为美国格式,这就是为什么问题是如此难以捉摸?我在一组微软程序员的其他地方遇到过这种类型的问题,他不知道另一组正在做什么。这是为什么有些功能总是有效,有些则不然?一些Microsoft程序员知道在哪里寻找正确的格式而其他人不知道?
More importantly, for me, can anyone suggest:
更重要的是,对我来说,任何人都可以建议:
我如何获得真实的日期或时间格式?
确定用户所选日期或时间显示格式的其他方法是什么?
BTW 1: I recall that thirty or so years ago I was told that the American military do not use month/day/year format; only American civilians use this format. Can anyone tell me if this is true?
顺便说一句:我记得大约三十年前我被告知美国军方不使用月/日/年格式;只有美国平民使用这种格式。谁能告诉我这是否属实?
BTW 2: The similar problem is with Excel colours. Excel holds its colours as "ggbbrr" while everybody else holds them as "rrggbb". The programmers for the .Net Excel inter-op were not told and and did not reverse the Excel colour number before using it to control the screen.
顺便说一句2:类似的问题是Excel颜色。 Excel将其颜色保持为“ggbbrr”,而其他人将其保留为“rrggbb”。在使用它来控制屏幕之前,没有告知.Net Excel inter-op的程序员并且没有反转Excel颜色编号。
1
I have mainly come up against formatting and date issues when opening text files which have been saved with different regional settings. Two useful cell properties for dealing with this are:
打开使用不同区域设置保存的文本文件时,我主要遇到格式和日期问题。处理这个问题的两个有用的单元属性是:
.Text
returns the cell value as it is displayed.Text返回显示的单元格值
.Value2
returns the unformatted cell value or date serial number..Value2返回未格式化的单元格值或日期序列号。
As you say, standard date and number formats depend on windows regional settings and this may not be desired behavior as the same workbook can display differently in different regions. MS introduced the regional code prefixes in number formats (circa Excel 2000?) which enforce consistent display if needed but they need to be explicitly selected.
如您所说,标准日期和数字格式取决于Windows区域设置,这可能不是所需的行为,因为同一工作簿可以在不同区域中以不同方式显示。 MS引入了数字格式的区域代码前缀(大约是Excel 2000?),如果需要,它会强制执行一致的显示,但需要明确选择它们。
If you really want to see a date or number as the user entered it, you could extract the contents of the .xlsx file looking at the worksheet cell format and the shared strings xml definitions which list the number formats in the saved workbook. I don't really see a need to do this though as the underlying value is stored internally as a serial number and this will not change.
如果您确实希望在用户输入日期或数字时查看日期或数字,则可以提取.xlsx文件的内容,查看工作表单元格格式和共享字符串xml定义,这些定义列出了已保存工作簿中的数字格式。我不认为有必要这样做,因为底层值在内部存储为序列号,这不会改变。
0
BTW 1: It's been almost 30 years since I was in the military... I worked on helicopters and I was taught to use a format such as this in the aircraft logbooks: 3 Apr 12. So, that's how I still write dates. This way, there's no wondering about 4/3/2012 - is it April 3 or March 4?
顺便说一句:我在军队已经差不多30年......我曾在直升飞机上工作,我被教导在飞机日志中使用这种格式:4月12日。所以,我仍然在写日期。这样,对于4/3/2012没有任何疑惑 - 是4月3日还是3月4日?
0
I hacked this: I rewrite the original data in a known format. it relies on DateSerial and TimeSerial:
我攻击了这个:我以已知格式重写原始数据。它依赖于DateSerial和TimeSerial:
'Google spreadsheet stores dates in USA format (MM/DD/YYYY). We're in Australia, using DD/MM/YYYY, so we need to swap them.
'
With dc 'the cell who contains a date in USA format.
d = .Value 'capture value in USA format
t = TimeValue(d)
.NumberFormat = "dd/mm/yyyy" 'set to OZ format, so Excel knows the values were swapped in its internal math.
.Value = DateSerial(Year(d), Month(d), Day(d)) 'DateSerial takes y,d,m. We swap Month and Day components, to get OZ format dates
.Value = .Value + TimeSerial(Hour(t), Minute(t), Second(t))
dc.Font.Bold = True ' We bold the cells that are swapped, for debugging
End With
End If