Question

I don't know what is wrong with the following string:

"Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + "  to  " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") +  ")"

I can't get the concatenated string. I am getting Report(29-Dec-2009. That's all and the rest gets left out from the string.

What is the reason?

Was it helpful?

Solution

Try this:

string filename = 
    String.Format(
        "Report({0:dd-MMM-yyyy} to {1:dd-MMM-yyyy})",
         System.DateTime.Now, System.DateTime.Now.AddMonths(-1));

EDIT: Since in your download box you got your filename broken in first whitespace, you could to try ONE of these:

filename = HttpUtility.UrlEncode(filename); // OR
filename = """" + filename + """";

Seems some browsers doesn't handles whitespaces very nicely: Filenames with spaces are truncated upon download. Please check it you can to download other filenames with whitespace in other sites.

OTHER TIPS

You need to assign it to something:

string s = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"

Update: I just saw your update to the question. How are you displaying the string? I'm guessing that you are displaying it in a GUI and the label is too short to display the complete text.

Try this:

string newstring = 
  string.Format(
                "Report ({0} to {1})", 
                System.DateTime.Now.ToString("dd-MMM-yyyy"), 
                System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
               );

What are you assigning the result to? It would be easier to read the code if you used string.Format

You are not assigning the concatenated result to anything, so can't use it:

string myConcatenated = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + ")";

Using this code...

string test = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " +
                   System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")";

I saw the following result.

Report(29-Dec-2009 to 29-Nov-2009)

It could be that the string is being truncated later on. Make sure that you set a breakpoint right after this code is run and check the value of the variable to which it is assigned (test in my case).

If, as in your previous question, you are using this value to create a file, it may be that it's the space before "to" that is causing the problem. Try to use:

"Report("
    + System.DateTime.Now.ToString("dd-MMM-yyyy")
    + "To"
    + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
    +  ")"

instead and see if that fixes it.

If that does fix it, you'll probably need to either figure out how to quote the entire file name so it's not treated as the three separate arguments, "Report(29-Dec-2009", "to" and "29-Nov-2009)". Or simply leave your reports names without spaces.

I'd choose the latter but then I'm fundamentally opposed to spaces in filenames - they make simple scripts so much harder to write :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top