Home » excel » Excel/CSV – How to stop excel automatically format string as date

Excel/CSV – How to stop excel automatically format string as date

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m writing a script to automatically insert dates for me in a database. My issue is that when I append a string to the CSV file, excel automatically converts it into a date.

Example of dates:

String dateOne = "2018-10-10"
String dateTwo = "2019-12-20"

And in excel these become

Row One - 10/10/2018
Row Two - 20/12/2019

I’ve read some similar sort of questions and tried adding a ‘ to the strings, so:

String dateOne = "'2018-10-10"
String dateTwo = "'2019-12-20"

But in Excel, these ‘ appear in the rows, whereas they should be “invisible”.

I’ve tried format cell, and the cells then appear the way I want, but on exiting then re-entering the CSV file, the format is changed back. Even if I were to do this, when I click the actual cell (displaying the format I want) the cell contents itself are the 10/10/2018 format, so clearly it’s just all “visual”.

Also, I’ve tried enclosing the strings in double quotations, an example below, but this doesn’t work, as well:

String dateOne = "\"2018-10-10\""

Any ideas what I can do to get the format I require?

How to&Answers:

You can put an equal sign “=” in front of your date, like ="2018-10-10".

If I just type that into an Excel cell, it works fine (even tested the result with the N() function which gives me a zero – so it is clearly considered text).

Maybe, this answer here might help you further.

Answer:

Visit the type box and change it from date to custom or simply edit the date format