0

Reading value like <YYYY><MM><DD> from DB, passing this value in to my date format like ("YYYYmmdd")

date = DateTime.Now;
YYMMVal = date.ToString(DateFormat);

I was using replace function to get my dateformat like below

strDateFormat = strDTF.ToString();
string LessThanFormat = strDateFormat.Replace("<", "");
string greaterThanFormat = LessThanFormat.Replace(">", "");
String yearFormats = greaterThanFormat.Replace("Y", "y");
String MonthFormat = yearFormats.Replace("m", "M");
string DateFormat = MonthFormat.Replace("D", "d");

or

StringBuilder srtbDateFormat = new StringBuilder(strDateFormat);
                               srtbDateFormat.Replace("<", "");
                               srtbDateFormat.Replace(">", "");
                               srtbDateFormat.Replace("Y", "y");
                               srtbDateFormat.Replace("m", "M");
                               srtbDateFormat.Replace("D", "d");

YYMMVal = date.ToString(srtbDateFormat.tostring());

What's the efficient way to replace the above string and match with date format?

6
  • <YYYY><MM><DD> is meant to be a format string? Commented Apr 7, 2014 at 23:39
  • Are you saying that "<YYYY><MM><DD>" is the input string and "YYYYmmdd" is the output string that you want? Commented Apr 7, 2014 at 23:40
  • what is wrong with your current approach? why do you want to change it ? Commented Apr 7, 2014 at 23:41
  • @ClickRick that correct. But my input string is not always "<YYYY><MM><DD>", it may be only "<YYYY>" or it may be null. if i found value then i change in to date format. Commented Apr 7, 2014 at 23:44
  • @Selman22 , i don't want to change , just wondering both do the same thing but am trying to understand whats the difference between two. Commented Apr 7, 2014 at 23:45

4 Answers 4

4

What you've got seems sensible and is fairly easy to understand. That's a good argument for leaving it as it is.

Also, if your format strings are limited to the year, month, and date components, plus the < and > characters, there are only so many combinations possible. You could set up a Dictionary<string, string> in advance and pre-seed it with every input string you anticipate, mapped to the desired output.

This assumes you're doing this in some kind of loop. If you only have to do it once, it doesn't matter.

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

Comments

2

You can use a Dictionary

string input = "<YYYY><MM><DD>";
var pairs = new Dictionary<char, char> { { 'Y', 'y' }, { 'm', 'M' }, { 'D', 'd' } };

string format = new string(
        string.Concat(input
            .Split(new[] {'<', '>'}, StringSplitOptions.RemoveEmptyEntries))
            .Select(x =>
            {
                if (pairs.ContainsKey(x))
                    return pairs[x];
                return x;
            }).ToArray()
        );

Comments

1

The simple way is to use the fact that the output from the Replace method is another string and chain the calls together:

        string inputDateFormat = "<YYYY><MM><DD>";
        string outputDateFormat = inputDateFormat.Replace("<", "")               
                                                 .Replace(">", "") 
                                                 .Replace("Y", "y")
                                                 .Replace("m", "M")
                                                 .Replace("D", "d");

Comments

0

If I understand correctly, you want to transform <YYYY><MM><DD> into YYYYmmdd. There are several ways of doing this, but if it is just about a date format string, why don't you provide the format string as needed from the beginning?

It does not matter which way you choose, <YYYY><MM><DD> will always be transformed into YYYYmmdd, so you always know the exact output of the function.

If your DATE comes in the format <YYYY><MM><DD> which means it looks like this: <2012><06><03> then you should take a look at celerno's answer.

Comments

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.