1

I have problem with export of date to my .csv file. I have this two strings:

string Time = DateTime.Now.ToString("H:mm:ss");
string Date = DateTime.Now.ToString("dd:HH:yyyy");

Time is working normal and I get to csv file right data, but Date is written to cell as "0,905324074074074". I found the problem, when creating the csv I have pName[2] = Date+";"; There is ; for shift to next cell, but it does this weird thing, i do not know why. Code for create csv:

        private void SaveFileMeasure(string fileNameWithPath, string textMer, bool deleteExist)
    {
        try
        {
            try
            {
                if (!deleteExist)
                {
                    File.AppendAllText(fileNameWithPath, textMer);
                }
                else
                {
                    File.WriteAllText(fileNameWithPath, textMer);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("Vytvoření souboru neproběhlo v pořádku.");
            }
        }
        finally
        {
        }
    }

Thank for help.

Dufino
  • 33
  • 6
  • I ran the code you posted, yet I got the output `21:11:2020` – Jakob Tinhofer Dec 21 '20 at 10:08
  • Maybe this can help : https://stackoverflow.com/questions/804118/best-timestamp-format-for-csv-excel – vernou Dec 21 '20 at 10:09
  • 1
    Please add the code you are using to write CSV. Maybe the bug is there. – Mikołaj Dec 21 '20 at 10:51
  • Thanks for the edition, but still there is no code for constructing CSV lines, especially those with expressions like `pName[2] = Date+";";`. What I'm trying to say: we need a minimal working example to reproduce this behaviour on our machines. – Mikołaj Dec 23 '20 at 12:15

1 Answers1

2

Your format of date is incorrect, Try "dd:MM:yyyy"

string date = DateTime.Now.ToString("dd:MM:yyyy");

.Net Fiddle

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44