1

Can anybody tell me the best approach or solution on how I would do the following?

I have a DateTime (as String) in following format:

string test = "21.12.2013";

How could I now remove all zero's from the month and day but still 'keep' the DateTime Logic:

//Example 1
string input = "06.10.2013" // 6th October
string output = "6.10.2013" //only remove '0' from the day

//Example 2
string input = "01.09.2012" // 1st September
string output = "1.9.2012" //remove from month and day

//Example 3
string input = "20.10.2011" // 20th October
string output = "20.10.2011" //should (must) stay!

I can also parse to DateTime if that would be make it easier but yeah I hope you got my idea...

Any help appreciated!

1
  • That's display and doesn't influence the way it's saved. Look for your correct representation here. Commented Dec 21, 2013 at 0:33

5 Answers 5

5

Parsing your string into DateTime and getting it back to string using ToString with desired patter seems to be the easiest way to go:

public static string GetRidOfZeros(string input)
{
    var dt = DateTime.ParseExact(input, "dd.MM.yyyy", CultureInfo.InvariantCulture);
    return dt.ToString("d.M.yyyy", CultureInfo.InvariantCulture);
}

Little testing, with your sample data:

var inputs = new List<string> { "06.10.2013", "01.09.2012", "20.10.2011" };
var outputs = new List<string> { "6.10.2013", "1.9.2012","20.10.2011" };

if(outputs.SequenceEqual(inputs.Select(d => GetRidOfZeros(d))))
    Console.WriteLine("Output is OK");
else
    Console.WriteLine("Collections does not match.");

Prints Output is OK.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx very much, I totally forgot bout ToString("d.M.yyyy") :(
4
DateTime.Parse(input).ToString("d.M.yyyy")

Comments

2

As you said, parsing to DateTime first would probably make things easier, since then you can just use:

myDateTime.ToString("d.M.yyyy");

Comments

2

When you parse it you can use ToString to format it any way you like:

var date = "06.10.2013";

DateTime parsed = DateTime.ParseExact(date, "dd.MM.yyyy", CultureInfo.InvariantCulture);

var noZerosHere = parsed.ToString("d.MM.yyyy");

Comments

-1

A decent "catch-all" method (that will work not just on DateTime but ANY kind of string) would be to split the string up, take out leading zeroes and then put the pieces back together again.

string input = "01.09.2012";
string[] values = input.Split(".");
string[] modifiedValues = values.Select(x => x.TrimStart('0');
string output = String.Join(".", modifiedValues);

You can adjust the delimiters for different representations of DateTime, e.g. those that use slashes (01/09/2012) or are written in a different order.

3 Comments

Why on earth would you do that? Don't you trust .NET's built-in DateTime?
IMO it's about "removing leading zeroes from substrings" as opposed to "reformatting a DateTime". I'd say it was nice to give a pure string manipulation answer, even though in this particular case ParseExact and ToString do the job too.
The title says "Remove '0' (Number) from DateTime Month and Day" so it is about reformatting a DateTime. Although your method probably works it's not really good advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.