Pergunta

I want to show Date and Time in format like (2-13-2014). How would I do it. Here is My code. I try this but it shows me Date and Time like this (2/13/2014). Please help me

private void buttonSave_Click(object sender, EventArgs e)
{
     dateTimePicker1.Format = DateTimePickerFormat.Custom;
     dateTimePicker1.CustomFormat = "dd-MM-yyyy";

     MessageBox.Show(dateTimePicker1.Value.ToShortDateString());
}
Foi útil?

Solução

I assume you want to show February 1, 2014 as 2-1-2014, so this code should work

MessageBox.Show(dateTimePicker1.Value.ToString("M-d-yyyy"));

You can also change "M-d-yyyy" to any format you like, see @Dumisani's comment below for the reference.

Outras dicas

The Format and CustomFormat of the DateTimePicker only relate to what's displayed in the DateTimePicker. The Value property is a DateTime and has no specific connection to the control. It's a binary value that has no format. If you want to display a DateTime in a particular format then you have to specify that format when you convert it to a string, regardless of where it came from in the first place. If you call ToShortDateString then you're going to use the default short date format for the system.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top