44
votes

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.

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. I need the ISO 8601 date format to be saved in a CSV file. How do I go about doing so?

7
Yes, the M/D/Y format is pretty useless - you can't sort by date on it, and violates what we learned in grade 1 - numbers have their most significant digits on the left, least significant on the right (hundred / tens / units, remember). So millennia (i.e. thousands of years) are obviously more significant than hundreds, tens, units of years, then tens of months, units of months, then tens of days, units of days. Dunno why people still use M/D/Y in the 21st century - it should be banned.Reversed Engineer

7 Answers

59
votes

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. Next, copy the ISO 8601 columns onto the originals (paste special: paste values only). Delete the calculated ISO 8601 columns which now have garbage in them because Excel sucks at ISO 8601 dates.

You now have a transformed CSV or TSV or whatnot. 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.

12
votes

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.

11
votes

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. E.g. with my formatting settings to German:

=TEXT(C2;"jjjjMMtt")

(Although the OS and Excel are set to English.)

6
votes

You could just jump straight to the nuclear option: Change your computer’s "Region and Language" settings to use the "yyyy-MM-dd" short date format.

6
votes

I copied and pasted @Dirk Bester's formula above,

=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:

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

And now it works like a charm. FYI.

1
votes

I believe you may well have created the right format in the .csv file.

But that Excel is automatically coercing that into a date value of the format you mention when you open the .csv file. 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.

1
votes

FWIW, none of the above worked for me in an Excel with 16th century dates, e.g. 26-08-1558 for the 26th of August 1558. So in order to convert that to an ISO date, I used:

=RIGHT(D2|4)&"-"&MID(D2|4|2)&"-"&LEFT(D2|2)