Taurus Omejia
Taurus Omejia

Reputation: 57

How do I format this Date object for a Windows Form App in c#?

I am making a simple Windows Forms App that allows the user to select any file and rename it to a default formatted name, based on what they entered in the text fields.

Example: "08_21_2015_DrJohnSmith_HowToSellHomes.mp3" -- http://postimg.org/image/ds52o0xu1/

The problem I am having is I don't know how to format the date text box so it saves the file in this format:

"08_21_2015_DrJohnSmith_HowToSellHomes.mp3"

Right now its saving the file in this format:

"Friday, August 21, 2015DrJohnSmith_HowToSellHomes.mp3"

I know this is very easy to do, but I am new to programming, and just started learning C# this week, so any help would be greatly appreciated. Here is my code. The link above of a image of the form.

    using System;
    using System.Windows.Forms;
    using System.IO;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void selectFileBtn_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
            }

            private void outputFileBtn_Click(object sender, EventArgs e)
            {
                var outputFolder = outputFileTB;
                folderBrowserDialog1.ShowDialog();
                outputFolder.Text = folderBrowserDialog1.SelectedPath;
            }

            private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
            {
                selectFileTB.Text = openFileDialog1.FileName;
            }

            private void saveBtn_Click(object sender, EventArgs e)
            {
                File.Move(selectFileTB.Text, outputFileTB.Text + "/" + dateTimePicker1.Text + speakerTitleTB.Text + firstNameTB.Text + 
                    lastNameTB.Text + "_" + messageTitleTB.Text + ".txt");     
            }
        }
    }

Upvotes: 0

Views: 248

Answers (2)

Dai
Dai

Reputation: 155503

I see you're using dateTimePicker1.Text - which returns the textual representation of the selected date value as it appears to the user, this is obviously not what you want :)

First, extract the actual DateTime value by using dateTimePicker1.Value instead of .Text, then use the DateTime.ToString() method and specify a format string to have complete control over what is generated, like so:

dateTime.ToString( "MM_dd_yyyy", CultureInfo.InvariantCulture )

When using .ToString (on dates, numbers, or most values really), if it has an overload that accepts an IFormatInfo use it to explicitly specify either CultureInfo.InvariantCulture or CultureInfo.CurrentCulture (as per current Microsoft FxCop guidelines). If you want consistent behaviour across multiple platforms be sure to use InvariantCulture because if you use CurrentCulture then the output text might not be what you expect - this isn't a problem with completely custom strings like "MM_dd_yyyy" but if you use standard format strings like "g" or "d" then you'll see wildly different results on different machines.

BTW, I strongly recommend you use yyyy-MM-dd as a date format instead of MM_dd_yyyy this is so that files can be sorted chronologically in a text list (such as in Windows' File Explorer), otherwise you'll have files sorted by month, then day, then by year, which makes no sense.

Finally, note that in format strings MM is for Months but mm is for Minutes.

Here's a reference:

Upvotes: 3

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Replace

dateTimePicker1.Text

with

dateTimePicker1.Value.ToString("dd_MM_yyyy", CultureInfo.InvariantCulture)

Upvotes: 3

Related Questions