• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2079
  • Last Modified:

convert string to mm/dd/yyyy format c#

i have a string variable set to "24/04/2009"

I want to convert  dynamcially  it to mm/dd/yyyy   --> 04/24/2009 and pass it a function which accepts a string
I dont want to set to variable to datetime .. Just as a string i want to convert to mm/dd/yyyy format  
0
dotnet0824
Asked:
dotnet0824
  • 8
  • 4
  • 3
  • +1
3 Solutions
 
RPCITCommented:
This method does not use any date formmatting. I hope it's what your looking for.
string inDate = "24/04/2009";
string[] dateParts = inDate.Split(new char[]{'/'});
string outDate = dateParts[1] + "/" + dateParts[0] + "/" + dateParts[2];
MessageBox.Show(outDate);

Open in new window

0
 
baijajusavCommented:

It's a bit of a hack, but if it's always in that format this should work:

String a = "24/04/2009";
        String[] parts = a.Split('/');
        String b = parts[1] + "/" + parts[0] + "/" + parts[2];
0
 
DanielWillmottCommented:

string s = "24/04/2009";
string c = s.Substring(s.IndexOf("/") + 1, 2) + "/" + s.Substring(0,s.IndexOf("/")) + "/" + s.Substring(s.LastIndexOf("/") + 1, 4);

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
baijajusavCommented:

Bizarre timing.
0
 
RPCITCommented:
we must have hit post at the same time...lol
0
 
RPCITCommented:
because it bugged me to do it with strings... I tried it again this way..  

this method DOES go through a datetime object.. so it has to be a real date.  (you can't use the 32nd day of January, or the 30th day of february)
string inDate = "24/04/2009";
MessageBox.Show(DateTime.Parse(inDate, new CultureInfo("en-GB")).ToShortDateString());

Open in new window

0
 
dotnet0824Author Commented:
Hi RPCIT & Usav

does it mean that only split function would solve the problem keeping it as a string. cant inbuilt functions like convert or whatever work in c# keeping it as string.
0
 
dotnet0824Author Commented:
No issues both would be awared equal points :-)
0
 
DanielWillmottCommented:
My answer doesn't use split :).
0
 
RPCITCommented:
By doing it with just string manipulation, you lose your strong typeing.  for instance.. if a date came across as '30/02/2009'  then you would never know that it was an impossible date.

My second attempt does a more "complete" job, and the third attempt (attached) allows for specific formatting if you need it.

In general.. for what you need the split would probably work ok, but it's just "feels" wrong (IMO) to not use real data types.
string inDate = "24/04/2009";
MessageBox.Show(DateTime.Parse(inDate, new CultureInfo("en-GB")).ToString("MM/dd/yyyy"));

Open in new window

0
 
RPCITCommented:
lol..  Split vs IndexOf, it's basically 6 of one, half a dozen of the other.
0
 
dotnet0824Author Commented:
ok lets say we have DateTime.Parse(inDate, new CultureInfo("en-GB")).ToString("MM/dd/yyyy"));

can I assign it to the string variable and pass it to a function which accepts a string ONLY
0
 
DanielWillmottCommented:
DateTime.Parse(inDate, new CultureInfo("en-GB")).ToString("MM/dd/yyyy")).ToString();
0
 
RPCITCommented:
>>>DateTime.Parse(inDate, new CultureInfo("en-GB")).ToString("MM/dd/yyyy"))<<< is a string.

so ...

String outval = DateTime.Parse(inDate, new CultureInfo("en-GB")).ToString("MM/dd/yyyy"));

callSomeMethod(string) ... would accept outval

so I guess the answer is yes.
0
 
RPCITCommented:
additional info for future searchers..  below is a link to a fairly complete list of culture codes for the CultureInfo() constructor.

http://sharpertutorials.com/list-of-culture-codes/
0
 
RPCITCommented:
I really wish you could edit...  I wanted to also say that you will need to add  the System.Globalizationusing namespace...  

using System.Globalization;
0
 
dotnet0824Author Commented:
thanks guys..
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now