Question Format Date in C#

Joined
Nov 8, 2020
Messages
8
Programming Experience
Beginner
Good morning all,

First of all, thank you for taking the time to read my post.
I have made a windows form, which works fine. The user will input the data and hit "insert", which saves the data as a CSV file. The data is then being importing into Tableau which needs the date format as yyyyMMdd. I cannot for the life of me figure out how to code C# to save the date as yyyyMMdd once saved as CSV. No matter what I do, it always saves it as MMddyyyy.
I have coded textBox11 to display todays date for the user. I simply need textBox11 date to save as yyyyMMdd when converted to a CSV file..
Below is the code I am using to export the file to CSV.

Thank you so much for taking the time to check this out.




private void textBox11_TextChanged(object sender, EventArgs e)
{
textBox11.Text = DateTime.Now.ToString("yyyy-MM-dd");

Excel.Workbook xlworkbook;
Excel.Worksheet xlworksheet;
object misValue = System.Reflection.Missing.Value;

xlworkbook = xlap.Workbooks.Add(misValue);

xlworksheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(1);

Excel.Sheets worksheets = xlworkbook.Worksheets;
worksheets[3].Delete();
worksheets[2].Delete();

xlworksheet.Cells[1, 1] = textBox1.Text;
xlworksheet.Cells[1, 2] = textBox11.Text = DateTime.Now.ToString("yyyy-MM-dd");
xlworksheet.Cells[1, 3] = textBox2.Text;
xlworksheet.Cells[1, 4] = textBox3.Text;
xlworksheet.Cells[1, 5] = textBox4.Text;
xlworksheet.Cells[1, 6] = textBox5.Text;
xlworksheet.Cells[1, 7] = textBox6.Text;
xlworksheet.Cells[1, 8] = textBox7.Text;
xlworksheet.Cells[1, 9] = textBox8.Text;
xlworksheet.Cells[1, 10] = textBox9.Text;
xlworksheet.Cells[1, 13] = textBox10.Text;
xlworksheet.Cells[1, 11] = comboBox1.Text;
xlworksheet.Cells[1, 12] = comboBox2.Text;

xlworkbook.SaveAs(@"\\172.21.4.14\BusinessLanding\BusinessLanding_Sync\operations_sync\FlightPlanning\savings_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv", Excel.XlFileFormat.xlCSVWindows);
xlworkbook.Close(true);
xlap.Quit();
 
This is not a C# question of how to save in specific date format. This is an Excel Object Model question of how to make Excel save a CSV with the date in a particular format.

Moving out of WinForms...
 
With the standard Excel UI, when you prefix text with a single quote, Excel treats the cell input as plain text and will not try to guess at the data type. Perhaps you can try the same. Otherwise, I'm quite sure there is a way with the Excel Object Model to tell it that cell B1 should be treated as "Text" and not as "General". (General is what tells Excel to try to guess at the data type.)

As an aside, your data looks simple enough that you could just write to a CSV file directly instead of using Excel. I assume there is a specific reason you are going through Excel, but just didn't bother telling us.
 
Hi Skydiver,

Thank you for the reply. I am going to simply close my account here.
Both times I have published a post for help I am always slammed with the dam rule book and then you leave a patronizing comment. I am a beginner here, I was not aware I needed to tell you the reason I am using excel.
For future reference, just have a quick think about how someone in need of help reads your messageS...
No need to reply, I am closing the account now, you dont need to waste your time explaining the rule book on closing an account..

Regards..
 
Jasus touchy touch. Calm down Ted. ?

No need to throw your toys out of your pram. You are being asked a valid question in order to best direct you to any possible and easier alternative. That is why we ask questions like why are you using Excel or why are you using a CSV file. And since we don't know what you are doing, we can only make assumptions and ask you those questions based on the limited information you provide. There is nothing patronising about the reply you received, but your attitude is shocking, and I find your reply more provocative and your invocation to create an argument as an excuse to close your account out of your own frustration to the replies you received as quite silly.
As an aside, your data looks simple enough that you could just write to a CSV file directly instead of using Excel.
This reply is clearly telling me that Skydiver is proposing an alternative solution, and nothing more. Please remember, we are all volunteers on here, and we do what we do for free and on our own watch. This isn't something you should take for granted, and I'd rather not work 14 hour shifts, and then come on here to offer my support and see your quibbling and moaning because you didn't get the answer you think you out-rightly deserve. You need to understand, that we often need to question your motives, and your approach to how you are doing something, and we do this in order to get a clear picture and so we can propose to you the best possible answer. I'm not going to hold your hand and beg you to stay on this forum. Your decision to leave this website is your own, and I respect that. We all do our best to be polite, professional and courteous to everyone. So don't be so vindictive and try to discredit someone by threatening to close your account because someone who is trying to help you by asking you for more elaborate answers regarding your reported issue has offended you somehow.

I ask you take some deep breaths before you next reply, and keep your accusations to your self, and keep your topic about your problem so we can help you by poring our energy into that instead of trivial bullshit like this. Now back on topic please.

Thank you.
 
@Shane_Nicholson - this should do it
Treat as text:
xlworksheet.Cells[1, 2] = textBox11.Text = "'" + DateTime.Now.ToString("yyyy-MM-dd");
xlworkSheet.get_Range("A2").NumberFormat = "@";
String interpolation tends to improve readability.
C#:
xlworksheet.Cells[1, 2] = textBox11.Text = $"'{DateTime.Now:yyyy-MM-dd}";
 

Latest posts

Back
Top Bottom