how to give a custom format to dateTimePicker in c# - c#

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());
}

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.

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.

Related

assign date to TextBox textmode="date" using code

when i assign the date to textBox of textmode= "date" ,it doesn't display and displays "mm/dd/yyy" although the textBox has the correct date in debugging and autopostBack is enabled .
DataTable dt= DepartMentDB.GetDepartmentById(ddlDepartment.SelectedValue.ToString());
string Managername = dt.Rows[0]["Dept_Manager"].ToString();
DateTime d = DateTime.Parse(dt.Rows[0]["Manager_hiredate"].ToString());
txtHiredate.Text = d.ToString("mm/dd/yyy");
You want "MM/dd/yyy" (notice the capital M. otherwise it would convert to minutes once its working). If it's still not working, use an invariant culture to force the conversion (since you are specifying the format directly). i.e.:
txtHiredate.Text = d.ToString("MM/dd/yyy", CultureInfo.InvariantCulture);
EDIT: Actually, it is because the textmode of date only support a specific format. Mainly yyyy-MM-dd or whatever the user specified in their culture settings. See https://stackoverflow.com/a/22747762/3419825
So either work with the text mode and go with yyyy-MM-dd, or remove the textmode, or use a framework like jQuery to add a mask
Cast your value to DateTime object

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.
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");
I don't understand why are you not just calling DateTime.ToString.
txtMFR.Text = myKeyVault.MFRDate.ToString();
If you want custom format, you can specify it like this
txtMFR.Text = myKeyVault.MFRDate.ToString("yyyy MMM dd HH:mm:ss");
First pick the format you would like to display it as:
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Then do:
txtMFR.Text = myKeyVault.MFRDate.ToString([put your selected format here]);
label1.Text = dateTimePicker1.Value.ToShortDateString();
This will do it.

Datetime format inconsistencies in C# and jQuery

I have an issue, i have a user based datetime format setting.
Now, i set the input format in the Global.asax as shown below
void MvcApplication_PreRequestHandlerExecute(object sender, EventArgs e)
{
if (userSettings.Settings.ContainsKey(Constants.DateFormat))
{
CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
cultureInfo.DateTimeFormat.DateSeparator = "/";
cultureInfo.DateTimeFormat.FullDateTimePattern = userSettings.Setting.Attributes[Constants.DateFormat];
cultureInfo.DateTimeFormat.LongDatePattern = userSettings.Setting.Attributes[Constants.DateFormat];
cultureInfo.DateTimeFormat.ShortDatePattern = userSettings.Setting.Attributes[Constants.DateFormat];
}
}
But when i bind the date values from a UI textbox to a property, i get only null values. what am i missing in this process.
the date format that the user has given is 'dd/mm/yy'. after MVC model binding is done, the input of '01/11/12' is bound as 11th january 2012.
Kindly suggest the issue and where will the fix be working out.
EDIT
Is it possible only to have month as first in a dateformat string like mm/dd/yy. is it not possible to have dd/mm/yy as a format string.
something like this?
cultureInfo.DateTimeFormat.LongDatePattern = CultureInfo.CreateSpecificCulture("id-ID");
The example is taking culture from Indonesia, we use dd/MM/yyyy format.
Perhaps you should look into a formal date format that can then be localized. We chose to go with ISO 8601 (which is now the default for WebAPI). Then, you can be confident on the client that you are going to read the correct date from the serialized format. However, then you need to localize the time (if needed).
Scott Hanselman has a good write up of the issues.
And this datetime javascript library looks interesting.

How to convert MM/dd/yyyy to yyyy/MM/dd in C# using globalization

I have a text box in which a user is supposed to enter a date in MM/dd/yyyy format. This date is stored as yyyy/MM/dd in the database.
I want the user to enter the date in MM/dd/yyyy format and later I want to convert it to yyyy/mm/dd so that I can query the database.
How can I convert the user input date MM/dd/yyyy to yyyy/mm/dd?
If you're certain of the input string's format, use DateTime.ParseExact specifying "MM/dd/yyyy", then return the DateTime using .ToString with the appropriate "yyyy/MM/dd" format string.
There's no need to reference anything in the System.Globalization namespace for this.
That said, your database should be storing dates with a datetime format, rather than a string, in which case the format doesn't matter as your DBMS should do the conversion for you.
You can parse the date and format the result:
string str = Date.Parse(myDate).ToString("yyyy/MM/dd");
Alternatively, if the current culture doesn't support that date format and you've already validated the input:
string items[] = myDate.Split('/');
string str = items[2] + "/" + items[0] + "/" + items[1];
When you said globalization, I assume you want the change to be automatic according to current culture
You can setup culture (at Global.asax.cs I suggest)
System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("the culture you want to set"); //
you do not need to touch any datetime thing, it just happened when you output them.
One thing is not the other.
Not sure why you want to insist on the text entered being MM/dd/yyyy, or why you haven't used a date time picker to make sure it is a date.
But at the point you get the content of the textbox as a date, parse it based on globalisation, or a set of acceptable formats. Now it is a date, and assuming it's a date in the database, format is irrelevant until you come to populate the text box, with some content from the DB, inwhich case you use DateTime's ToString method witha globalisation parameter, usually CultureInfo.CurrentCulture, or if you've got away with it CultureInfo.InvariantCulture.
If it's a string in the DB, then this is the least of your problems.
They key point is if you use dates properly, format is only relevant for Parse, and ToString type methods.
i am using
IFormatProvider culture=new CultureInfo("en-GB",true);
sqlcommand cmd=new sqlcommand("query",con);
cmd.Parameters.AddWithValue("#date",DateTime.Parse(txtdate.Text.Trim(),culture,DateTimeStyles.NoCurrentDateDefault).Date);
for this to work the format property must be set to dd/MM/yyyy
and text box read only property must be false

DateTimePicker: pick both date and time

Is it possible to use DateTimePicker (Winforms) to pick both date and time (in the dropdown)? How do you change the custom display of the picked value? Also, is it possible to enable the user to type the date/time manually?
Set the Format to Custom and then specify the format:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM/dd/yyyy hh:mm:ss";
or however you want to lay it out. You could then type in directly the date/time. If you use MMM, you'll need to use the numeric value for the month for entry, unless you write some code yourself for that (e.g., 5 results in May)
Don't know about the picker for date and time together. Sounds like a custom control to me.
It is best to use two DateTimePickers for the Job
One will be the default for the date section and the second DateTimePicker is for the time portion. Format the second DateTimePicker as follows.
timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
timePortionDateTimePicker.ShowUpDown = true;
The Two should look like this after you capture them
To get the DateTime from both these controls use the following code
DateTime myDate = datePortionDateTimePicker.Value.Date +
timePortionDateTimePicker.Value.TimeOfDay;
To assign the DateTime to both these controls use the following code
datePortionDateTimePicker.Value = myDate.Date;
timePortionDateTimePicker.Value = myDate.TimeOfDay;
Unfortunately, this is one of the many misnomers in the framework, or at best a violation of SRP.
To use the DateTimePicker for times, set the Format property to either Time
or Custom (Use Custom if you want to control the format of the time using
the CustomFormat property). Then set the ShowUpDown property to true.
Although a user may set the date and time together manually, they cannot use the GUI to set both.
DateTime Picker can be used to pick both date and time that is why it is called 'Date and Time Picker'. You can set the "Format" property to "Custom" and set combination of different format specifiers to represent/pick date/time in different formats in the "Custom Format" property. However if you want to change Date, then the pop-up calendar can be used whereas in case of Time selection (in the same control you are bound to use up/down keys to change values.
For example a custom format " ddddd, MMMM dd, yyyy hh:mm:ss tt " will give you a result like this : "Thursday, August 20, 2009 02:55:23 PM".
You can play around with different combinations for format specifiers to suit your need e.g MMMM will give "August" whereas MM will give "Aug"
Go to the Properties of your dateTimePickerin Visual Studio and set Format to Custom. Under CustomFormat enter your format. In my case I used MMMMdd, yyyy | hh:mm
You can get it to display time. From that you will probably have to have two controls (one date, one time) the accomplish what you want.
I'm afraid the DateTimePicker control doesn't have the ability to do those things. It's a pretty basic (and frustrating!) control. Your best option may be to find a third-party control that does what you want.
For the option of typing the date and time manually, you could build a custom component with a TextBox/DateTimePicker combination to accomplish this, and it might work reasonably well, if third-party controls are not an option.
If you need (24 hours) military time. You should use "HH" instead of "hh".
"MM/dd/yyyy HH:mm"

Categories

Resources