Date format that is guaranteed to be recognized by Excel - excel

We're exporting our analytics reports in various formats, among them CSV. For some clients this CSV finds it's way into Excel.
Inside the CSV file one of the columns is a Date, for example
"Start Date","Name"
"07-04-2010", "Maxim"
Excel has trouble parsing this date format, obviously depending on the Locale of the user. Is "07" is the day or the month...
Could you recommend some textual format for a Date field that excel will not have trouble parsing? I'm aiming at the most fail safe option possible. I would settle for some escape sequence that will cause excel to avoid parsing the text in the column altogether.
Thanks for helping,
Maxim.

You have two options. Go with the month as a string and the year as 4 digits, or use ISO formatting: yyyy-mm-dd.

If you format your dates as follows in the csv output, Excel will parse the content exactly as a date (other columns for realism only)
43,somestring,="03/03/2003",anotherval
55,anotherstring,="01/02/2004",finalval
so add ="{date}" and it parses as date!

Related

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.
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?
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.
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)
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.)
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.
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.
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.
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)
=DATEVALUE(LEFT(A1;10))
I used that

Format multiple date entries as strings

I have an Excel file storing a thousand lines of dates. Each date seems to be (auto)formatted as a Date. A (PHP Excel) parser I'm using (really can't update/use another one) is parsing this to a string which will occur in the number of days till 1900.
Is there a way to format the values in Excel being simple text "08.03.1991" to get this file parsed correctly?
I could add a quote: "'08.03.1991" but I need an (Excel-based) one-action-solution for all the thousand lines.
Remark: Since this is a file of a user I can't just write simple VBA-Script or so to handle this since there will be new files in the future and the User needs to be able to solve this alone.
I admit I am not quite sure what you have and what you want but it may be worth trying: Select column of dates, apply Text to Columns with Tab as delimiter and in step 3 of 3 select Text.
You could use the TEXT function like this:
=TEXT(A1,"dd.mm.yyyy")
For more details have a look here

FIxing MS Excel date time format

A reporting service generates a csv file and certain columns (oddly enough) have mixed date/time format , some rows contain datetime expressed as m/d/y, others as d.m.y
When applying =TYPE() it will either return 1 or 2 (Excel will recognize either a text or a number (the Excel timestamp))
How can I convert any kind of wrong date-time format into a "normal" format that can be used and ensure some consistency of data?
I am thinking of 2 solutions at this moment :
i should somehow process the odd data with existing excel functions
i should ask the report to be generated correctly from the very beginning and avoid this hassle in the first place
Thanks
Certainly your second option is the way to go in the medium-to-long term. But if you need a solution now, and if you have access to a text editor that supports Perl-compatible regular expressions (like Notepad++, UltraEdit, EditPad Pro etc.), you can use the following regex:
(^|,)([0-9]+)/([0-9]+)/([0-9]+)(?=,|$)
to search for all dates in the format m/d/y, surrounded by commas (or at the start/end of the line).
Replace that with
\1\3.\2.\4
and you'll get the dates in the format d.m.y.
If you can't get the data changed then you may have to resort to another column that translates the dates: (assumes date you want to change is in A1)
=IF(ISERR(DATEVALUE(A1)),DATE(VALUE(RIGHT(A1,LEN(A1)-FIND(".",A1,4))),VALUE(MID(A1,FIND(".",A1)+1,2)),VALUE(LEFT(A1,FIND(".",A1)-1))),DATEVALUE(A1))
it tests to see if it can read the text as a date, if it fails, then it will chop up the string, and convert it to a date, else it will attempt to read the date directly. Either way, it should convert it to a date you can use

Figuring Excel date format programmatically

I have a program that gets input in the form of an excel which it reads by querying (select * into a dataset).
The excel is created by the customer.
The excel contains a date column.
The problem is, that when looking into the dataset, the date column format is sometimes like mm/dd/yyyy and sometimes dd/mm/yyyy. (1/25/1970) (25/1/1970)
I guess it depends on the locale on the machine which the excel was made.
How can my program know what is the date format in the excel?
Not sure whether your question is about output or input...
For output:
You did not mention the database type. But the rule is: make your dates the american way: mm/dd/yyyy. For Jet, I generally use Format(MyDate, ""\#dd\/mm\/yyyy\#"")
("\" is the escape character, it makes sure the next character is output "as is")
For input:
Try reading the cell format ? range("a1").NumberFormat
However on my PC, with French setup a date displayed as 15/2/2011 has a returned format = m/d/yyyy so little use here !

Best timestamp format for CSV/Excel?

I'm writing a CSV file. I need to write timestamps that are accurate at least to the second, and preferably to the millisecond. What's the best format for timestamps in a CSV file such that they can be parsed accurately and unambiguously by Excel with minimal user intervention?
For second accuracy, yyyy-MM-dd HH:mm:ss should do the trick.
I believe Excel is not very good with fractions of a second (loses them when interacting with COM object IIRC).
The earlier suggestion to use "yyyy-MM-dd HH:mm:ss" is fine, though I believe Excel has much finer time resolution than that. I find this post rather credible (follow the thread and you'll see lots of arithmetic and experimenting with Excel), and if it's correct, you'll have your milliseconds. You can just tack on decimal places at the end, i.e. "yyyy-mm-dd hh:mm:ss.000".
You should be aware that Excel may not necessarily format the data (without human intervention) in such a way that you will see all of that precision. On my computer at work, when I set up a CSV with "yyyy-mm-dd hh:mm:ss.000" data (by hand using Notepad), I get "mm:ss.0" in the cell and "m/d/yyyy hh:mm:ss AM/PM" in the formula bar.
For maximum information[1] conveyed in the cells without human intervention, you may want to split up your timestamp into a date portion and a time portion, with the time portion only to the second. It looks to me like Excel wants to give you at most three visible "levels" (where fractions of a second are their own level) in any given cell, and you want seven: years, months, days, hours, minutes, seconds, and fractions of a second.
Or, if you don't need the timestamp to be human-readable but you want it to be as accurate as possible, you might prefer just to store a big number (internally, Excel is just using the number of days, including fractional days, since an "epoch" date).
[1]That is, numeric information. If you want to see as much information as possible but don't care about doing calculations with it, you could make up some format which Excel will definitely parse as a string, and thus leave alone; e.g. "yyyymmdd.hhmmss.000".
"yyyy-MM-dd hh:mm:ss.000" format does not work in all locales. For some (at least Danish) "yyyy-MM-dd hh:mm:ss,000" will work better.
I believe if you used the double data type, the re-calculation in Excel would work just fine.
Go to the language settings in the Control Panel, then Format Options, select a locale and see the actual date format for the chosen locale used by Windows by default. Yes, that timestamp format is locale-sensitive. Excel uses those formats when parsing CSV.
Even further, if the locale uses characters beyond ASCII, you'll have to emit CSV in the corresponding pre-Unicode Windows "ANSI" codepage, e.g. CP1251. Excel won't accept UTF-8.
"yyyy-mm-dd hh:mm:ss.000" format does not work in all locales. For
some (at least Danish) "yyyy-mm-dd hh:mm:ss,000" will work better.
as replied by user662894.
I want to add: Don't try to get the microseconds from, say, SQL Server's datetime2 datatype: Excel can't handle more than 3 fractional seconds (i.e. milliseconds).
So "yyyy-mm-dd hh:mm:ss.000000" won't work, and when Excel is fed this kind of string (from the CSV file), it will perform rounding rather than truncation.
This may be fine except when microsecond precision matters, in which case you are better off by NOT triggering an automatic datatype recognition but just keep the string as string...
So, weirdly excel imports a csv date in different ways. And, displays them differently depending on the format used in the csv file. Unfortunately the ISO 8061 format comes in as a string. Which prevents you from possibly reformatting the date yourself.
All the ones the do come in as a date... contain the entire information... but they format differently... if you don't like it you can choose a new format for the column in excel and it will work. (Note: you can tell it came in as a valid date/time as it will right justify... if it comes in as a string it will left justify)
Here are formats I tested:
"yyyy-MM-dd" shows up as a date of course when opened in excel. (also "MM/dd/yyyy" works)
"yyyy-MM-dd HH:mm:ss" default display format is "MM/dd/yyyy HH:mm" (date and time w/out seconds)
"yyyy-MM-dd HH:mm:ss.fff" default display format is "HH:mm:ss" (time only w/ seconds)
As for timezones. I have to store the UTC offset as seconds from UTC that way formulas in Excel/OpenOffice can eventually localize datetimes. I found this to be easier than storing any number that has a 0 in front of it. -0900 didn't parse well in any spreadsheet system and importing it was nearly impossible to train people to do.
Try MM/dd/yyyy hh:mm:ss a format.
Java code to create XML file.
xmlResponse.append("mydate>").append(this.formatDate(resultSet.getTimestamp("date"),
"MM/dd/yyyy hh:mm:ss a")).append("");
public String formatDate(Date date, String format)
{
String dateString = "";
if(null != date)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateString = dateFormat.format(date);
}
return dateString;
}
I wrote my timestamps to the CSV file as yyyy-mm-dd hh:mm:ss.
In Excel 365 I create a blank workbook, go to the Data tab and choose "From Text/CSV.
A dialog opens with a preview, which weirdly leaves the first column of timestamps alone, but shows it is going to convert the second column.
You can either "Load" the data, or "Transform Data", which is what I choose.
Now you're in the "Power Query Editor" and you can massage how Excel will bring the columns in.
In my case I undo any automatic changes it made, and tell it both columns are Data Type "Date/Time" using a little button on the ribbon.
Pressing Close & Load brings it into Excel as a data source that you can sort etc.
Given a csv file with a datetime column in this format: yyyy-mm-dd hh:mm:ss
Excel shows it in this format: dd/mm/yyyy hh:mm
e.g. 2020-05-22 16:40:55 shows as 22/05/2020 16:40
This is evidently determined by the Short date and Short time format selected in Windows; for example, if I change the Short date format in Windows to yyyy-mm-dd, Excel shows 2020-05-22 16:40.
Annoyingly, I can't find any way to make Excel show the seconds automatically (I have to manually format the column in Excel). But if the csv file includes a time column in hh:mm:ss format (e.g. 16:40:55), that's what shows in Excel, including the seconds.
I would guess that ISO-format is a good idea. (Wikipedia article, also with time info)

Resources