I have a string like this 25/05/2016 now I want to get a string like this 25/05/16 and like this 25-05-2016 how can I do this in c#.
Asked
Active
Viewed 217 times
4 Answers
16
Instead of string manipulation, parse the Date properly:
var date = DateTime.Parse("25/05/2016");
var date1 = date.ToString("dd/MM/yy"); <-- 25/05/16
var date2 = date.ToString("dd-MM-yyyy"); <-- 25-05-2016

Nitin
- 18,344
- 2
- 36
- 53
-
I got a small problem here `String was not recognized as a valid DateTime.` in first line – bill Apr 21 '16 at 06:21
-
@bill - You need to make sure that you're using the right globalization culture for parsing. – Enigmativity Apr 21 '16 at 06:23
-
0
Nitin's answer is the best for your problem.
But if it were not an date you could convert the String to an byte array, modify the characters you need to and then convert it back to string.

Chakratos
- 49
- 1
- 10
0
You could always just use string manipulation:
var source = "25/05/2016";
var result1 = String.Join("/", source.Split('/').Select(x => x.Substring(x.Length - 2, 2)));
var result2 = source.Replace("/", "-");
These give the correct results:
25/05/16 25-05-2016

Enigmativity
- 113,464
- 11
- 89
- 172