Search code examples
c#datetimedatetimepickerpicker

C# Date Time Picker to Text?


Im trying to get a text from a file into date format for a label.

What i currently have works great for a DateTimePicker however im wanting to now use a label to display the date rather than a DateTimePicker.

This is what currently works when getting the value to a DateTimePicker:

        dateTimeMFR.Value = this.myKeyVault.MFRDate;

and this is what im attempting to make work in a label:

        DateTimePicker myDate = new DateTimePicker();
        myDate.Value = myKeyVault.MFRDate;
        txtMFR.Text = myDate.Text;

Thanks for any help on the matter.


Solution

  • It depends on the format which you want to show the date in. If it should be default user format, then this:

    txtMFR.Text = myKeyVault.MFRDate.ToString();
    

    is sufficient. You can also manually format DateTime as date or time by calling ToShortTimeString or ToShortDateString or combinations of them. Or you can provide one of the predefined string formats as explained here or here. For example:

    txtMFR.Text = myKeyVault.MFRDate.ToString("T");