Hi everybody,

I am trying to convert a string to a Date format.
So what i actually want to do is, i want to convert any format of date to DD/MM/YYYY format. For example the format can be d/m/yyyy, m/d/yyyy, mm/dd/yyyy, dd/mmm/yyyy, etc......

I tried using CDATE(value), datetime.convert(value), i even tried
value.ToString("dd/MM/yyyy")

Suppose if the date that i am trying to convert is 5/20/2008, this date is in the format m/dd/yyyy. when i try to convert it as CDATE("5/20/2008"), it shows an error saying "the value is not correct". I think while converting it takes the date as month, so we don't have a month as 20... so i think that's why it shows the error

Can anybody help me to solve this....
The final thing that i want to do is i need to convert any date format to dd/MM/yyyy.
Please help me solve this

Thanks

I did not test this, but I think you can use this
DateTime dt;
dt = Convert.ToDateTime("5/20/2008");

I am assuming your are in C#. VB is basically the same thing

hope it works regards

I could be wrong as I've not done it, but I think this is more than a simple conversion tool can handle -- if I'm understanding your question, you don't want to convert one type of date, but ANY date format.

First question is to identify what date format the string was entered in as -- then have a sub routine where you assign the month / day / year values -- I would have the user input the seperation value (such as "/" or "-").

Then you can write a subroutine to pull out each of the values with an if then loop reading each character of the string.

Once you have all your individual values, it's just as simple as using the date function as jbisono suggests;

dtText = MonthVariable + "/" + "DayVariable + "/" + YearVariable
dt = Convert.ToDateTime(dtText)

GL!

Hi jbisono,

Thank you for your reply, i even tried this but the result was the same.

Thanks

Oh well sorry to hear that, Lets keep try.

String MyString;
 MyString = "1999-09-01 21:34 PM";
 //MyString = "1999-09-01 21:34 p.m.";  //Depends on your regional settings

 DateTime MyDateTime;
 MyDateTime = new DateTime();
 MyDateTime = DateTime.ParseExact(MyString, "dd/MM/yyyy",null);

Hi beuki,

Thank you for your reply. Yes beuki you are right, i just don't want to convert one date format to dd/MM/yy, but any date format to dd/MM/yyyy.The things will be easy if i can check in what format i get the date. But how can we find in what format we get the date.
For example let's say the date that i get is 5/5/2008 (or) 05/05/2008. So from this date how can we say that this one is month and this one is date. It's not just for this date but take any date upto 12.

I will tell you the real situation i am in now. I am developing a windows based application.By default the DateTimePicker shows the date format that is in our system's regional settings. But in my application the datetimepicker needs to show the date in the format dd/MM/yyyy. So i changed the datetimepicker's Format property to 'Custom' and i entered the 'CustomFormat' type as dd/MM/yyyy. So now that datetimepicker shows the date as i want. But still in my regional settings the date format is M/d/yyyy. So there are stages in my application where i fetch the date from the database and show it on the application. Now the date that i get from the database is in the format M/d/yyyy, i need to convert this date to dd/MM/yyyy and display it on my application. But i am not able to do so.

Kindly help.

Thanks

Hi jbisono,

Thanks for ur reply, but sorry to say i even tried this and it didn't work. Lets say the date that i get is in the format M/d/yyyy, and the date is 5/5/2008.. the method u mentioned works fine and it converts the date to dd/mm/yyyy. But if the date is 5/14/2008, then it shows the error as the value for date is not correct. This is where i am stuck.

Hi jbisono,

Thanks for ur reply, but sorry to say i even tried this and it didn't work. Lets say the date that i get is in the format M/d/yyyy, and the date is 5/5/2008.. the method u mentioned works fine and it converts the date to dd/mm/yyyy. But if the date is 5/14/2008, then it shows the error as the value for date is not correct. This is where i am stuck.

Yes because when you say 5/14/2008, the code assume 14 as month and there is not 14 month so change the dd/mm/yyyy to MM/dd/yyyy it should work.

regards

This article has been dead for over six months. Start a new discussion instead.