简体   繁体   English

"如何将 Excel 中的日期转换为 ISO 8601 格式"

[英]How to convert a date in Excel to ISO 8601 format

I am trying to save a date format in YYYY-MM-DD, for example, 2014-09-01 as a CSV file, but the format reverts back to the M/D/YYYY format when I do.我正在尝试将日期格式保存为 YYYY-MM-DD,例如 2014-09-01 作为 CSV 文件,但是当我这样做时,格式会恢复为 M/D/YYYY 格式。

I tried converting the date as a string in Excel, but every time I open up the CSV file, it's back to the M/D/YYYY format.我尝试在 Excel 中将日期转换为字符串,但每次打开 CSV 文件时,它都会返回 M/D/YYYY 格式。 I need the ISO 8601 date format to be saved in a CSV file.我需要将 ISO 8601 日期格式保存在 CSV 文件中。 How do I go about doing so?我该怎么做呢?

The basic function is: 基本功能是:

=TEXT(A1,"yyyy-mm-ddThh:MM:ss")

Use this to convert your Excel date columns to separate ISO 8601 date columns. 使用此选项可将Excel日期列转换为单独的ISO 8601日期列。 Next, copy the ISO 8601 columns onto the originals (paste special: paste values only). 接下来,将ISO 8601列复制到原件上(粘贴特殊:仅粘贴值)。 Delete the calculated ISO 8601 columns which now have garbage in them because Excel sucks at ISO 8601 dates. 删除现在有垃圾的计算的ISO 8601列,因为Excel糟透了ISO 8601日期。

You now have a transformed CSV or TSV or whatnot. 您现在有一个转换的CSV或TSV或诸如此类的东西。 Just save as the original format and ignore the stupid Excel whining about it not being its native file format and how you are going to "lose out" somehow by saving as CSV file and try not to think about the hours of your life Microsoft has stolen with that dumb dialog. 只需保存为原始格式,并忽略愚蠢的Excel抱怨它不是它的本机文件格式以及如何通过保存为CSV文件以某种方式“丢失”并尽量不去考虑微软偷窃的生活时间与那个愚蠢的对话。

As a note of caution for non-English users. 作为非英语用户的注意事项。 It just took me a while to figure out, that the format string is sensitive to your regional settings / locale. 我花了一段时间才弄明白,格式字符串对您的区域设置/区域设置很敏感。 Eg with my formatting settings to German: 例如,我的格式设置为德语:

=TEXT(C2;"jjjjMMtt")

(Although the OS and Excel are set to English.) (虽然OS和Excel设置为英语。)

I copied and pasted @Dirk Bester's formula above, 我复制并粘贴了@Dirk Bester的公式,

=TEXT(A1,”yyyy-mm-ddThh:MM:ss”)

but it wouldn't work, Excel 2010 complaining high and low -- that is, until I changed the quote marks from some kind of "smart quote" to plain old ASCII 0x22 quote marks: 但它不起作用,Excel 2010抱怨高低 - 也就是说,直到我将引号从某种“智能引用”更改为普通的旧ASCII 0x22引号:

=TEXT(A1,"yyyy-mm-ddThh:MM:ss")

And now it works like a charm. 而现在它就像一个魅力。 FYI. 仅供参考。

您可以直接跳到核选项:更改计算机的“区域和语言”设置以使用“yyyy-MM-dd”短日期格式。

You can set up a cell, example : 您可以设置一个单元格,例如:

Right click on cell > Cell format > Category > Custom > Type > write this : 右键单击单元格>单元格格式>类别>自定义>类型>写入:

aaaa-mm-jjThh:mm:ss (french format here)
yyyy-mm-ddThh:MM:ss (english format)

Sorry for my english. 对不起我的英语不好。

I believe you may well have created the right format in the .csv file. 我相信你可能已经在.csv文件中创建了正确的格式。

But that Excel is automatically coercing that into a date value of the format you mention when you open the .csv file. 但是Excel会自动将其强制转换为打开.csv文件时提到的格式的日期值。 A solution may be to import the .csv file rather than open it, and at step 3 of Get External Data, From Text, ensure that Text is selected for Column data format, where appropriate. 解决方案可能是导入.csv文件而不是打开它,并且在获取外部数据的步骤3中,从文本中确保在适当的情况下为列数据格式选择了文本。

FWIW,以上都没有在16世纪日期的Excel中为我工作,例如1558年8月26日的26-08-1558。所以为了将其转换为ISO日期,我使用了:

=RIGHT(D2|4)&"-"&MID(D2|4|2)&"-"&LEFT(D2|2)
=DATEVALUE(LEFT(A1;10))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM