0

I have simple quesion but I have no idea how to solve it...

I have this string

RegisterDate = "25-05-2013"; 

I get this value via input. I want somehow to make this string be

RegisterDate = "2013-05-25"; 

My question is how could I do it?

Thanks

2

3 Answers 3

5

Try to covert it to real date and convert it back again to string with your desired format.

RegisterDate = "25-5-2013"; DateTime _date = DateTime.ParseExact(RegisterDate, "M-d-yyyy", CultureInfo.InvariantCulture); RegisterDate = _date.ToString("yyyy-MM-dd"); 
Sign up to request clarification or add additional context in comments.

4 Comments

you have an error, the format should be d-M-yyyy. the string date isn't in US format
Hi, thank you, but I get this error message "The name 'CultureInfo' does not exist in the current context "
Press Ctrl+. and select one of the options. Or put using System.Globalization; in your source file.
This assumes that the string represents a date which probably is a safe assumption given the name RegisterDate.
4

There are many ways to do what you want. Here I split the string into the three parts between the dashes, reverse it and then rejoin the parts again:

var registerDate = "25-5-2013"; registerDate = String.Join("-", registerDate.Split('-').Reverse()); 

If the string represents a date you can also use date parsing and formatting methods. Another option is to use regular expressions.

Comments

2

Use split: this way

string[] dateItems = RegisterDate.Split('-'); RegisterDate = dateItems[2] + "-" +dateItems[1] + "-" + dateItems[0]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.