views:

295

answers:

7

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?

+1  A: 

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

Giorgi
+3  A: 

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.

Mark Byers
+2  A: 

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")
               );
Pablo Santa Cruz
+1  A: 

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

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

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.

Rubens Farias
i am passing this string filename to my report header and it doesnt seem to take my file name..
Pandiya Chendur
@Rubens I want file name to be Report(29-Dec-2009 to 29-Nov-2009) but my filename in Save/Open Dialog shows Report(29-Dec-2009..
Pandiya Chendur
@ruben why this file name doesnt show in my open/save dialog box as i am exporting repots for last one month
Pandiya Chendur
@rubens none seems to work for me
Pandiya Chendur
@rubens firefox seems to ignore file name from first white space
Pandiya Chendur
so you got a browser problem, not a C#; you can to replace all whitespaces from your filename with `_`, like `filename = filename.Replace(" ", "_");`
Rubens Farias
A: 

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).

Scott Munro
A: 

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 :-)

paxdiablo