Reputation: 300
I have a datetimepicker control ploted on datagridview in windows application. I want the datetimepicker format to be exactly like the system datetime format. Is it possible?
Upvotes: 1
Views: 5125
Reputation: 300
I was able to solve my problem this way:
DateTimeFormatInfo sysFormat = CultureInfo.CurrentCulture.DateTimeFormat;
string sysdatepick = sysFormat.ShortDatePattern;
string systimepick = sysFormat.LongTimePattern;
dateTimePicker1.CustomFormat = sysdatepick + systimepick;
Upvotes: 0
Reputation: 2043
DateTimePicker can be used to set a DateTime variable using it's Value property:
DateTime myDateTime = dateTimePicker1.Value;
The value property is of the same type as system DateTime. How you format you dated when converting to a string is up to you. You can use:
myDateTime.ToLongDateString();
outputs: "22 August 2013"
myDateTime.ToLongTimeString();
outputs: "13:20:30"
myDateTime.ToShortDateString();
outputs: "22/08/2013" (in the UK) or "08/22/2013" elsewhere depending on regional settings
myDateTime.ToShortTimeString();
outputs: "13:20"
Or do custom formatting such as:
myDateTime.ToString("dd-MMM-yyyy HH:mm:ss")
outputs: "21-Aug-2013 13:20:30"
To set the displayed format of the DateTimePicker please see Bagzli's answer.
Upvotes: 0
Reputation: 9947
You can set it using
dateTimePicker1.Format = DateTimePickerFormat.Time
Time property will set the format using the format by user's system
Upvotes: 1
Reputation: 6597
"The date/time format is based on the user's regional settings in their operating system." Which means that it should already be in the format that your system is using.
Here is a link for the default .net (c#) date time picker, how to set up the format in a specific way: http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.format.aspx
Example:
public void
SetMyCustomFormat()
{
// Set the Format type and the CustomFormat string.
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MMMM dd, yyyy - dddd";
}
Upvotes: 0