Reputation: 387
I am trying to make a program that will store the date in the database with the format of dd/MM/yyyy.I googled and saw same results. I tried coding it but it doesnt work. The input date is in the pattern of dd/MM/yy using datetimepicker's custom format. After manipulating the string a little bit to remove padding zeroes, the string returned is MM/dd/yyyy.
Here is my code
System.Windows.Forms.MessageBox.Show(Cdate.Text + "CDATE"); //shows dd/MM/yyyy
string[] datecut = new string[4];
datecut = Cdate.Text.Split('/');
DateTime cdate = DateTime.ParseExact(datecut[0].PadLeft(2, '0') + '/' + datecut[1].PadLeft(2, '0') + '/' + datecut[2], "dd/MM/yyyy", null);
EC.setCDate(cdate.ToShortDateString());
System.Windows.Forms.MessageBox.Show(cdate.ToShortDateString()); //shows MM/dd/yyyy
datecut = Cdate.Text.Split('/');
DateTime date = DateTime.ParseExact(datecut[0].PadLeft(2, '0') + '/' + datecut[1].PadLeft(2, '0') + '/' + datecut[2], "dd/MM/yyyy", null);
EC.setDate(date.ToShortDateString());
System.Windows.Forms.MessageBox.Show(date.ToShortDateString());
Upvotes: 0
Views: 10064
Reputation: 415725
store the date in the database with the format of dd/MM/yyyy
No, no, NO. Your database will have a data type specifically dedicated to date fields. You should use that type. This date type does not store the date value in any human-readable format. It will store the date in a compact binary format. Your query or database tool only shows you a human-readable format for convenience.
If you can give an example of where you save the date to the database, and what kind of database you're using, we can show you how to do this without formatting your date at all.
Upvotes: 4
Reputation: 1600
Sounds like you have a datepicker and you are attempting to work with the string representation of the selected date. If that's the case I would not do that. Date picker has a property called value which is a DateTime Object representing the chosen date, in which you could literatlly pass to your EC.setDate Method like so.
EC.setDate(dateTimePicker.Value.ToString("dd/MM/yyyy"));
Upvotes: 0
Reputation: 2368
Generally you use the DateTime.TryParse method to parse a string. (http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx) Once you have the string as a DateTime object, you can print it back out however you like using DateTime.toString() (http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx)
Upvotes: 0
Reputation: 816
Have you tried:
String Date = DateTime.Now.ToString("dd/MM/yyyy");
Upvotes: 1