0

I'm completely new to C# Asp.Net, and having trouble formatting the following string:

string defTo = string.Format (@"{0:yyyy\/MM\/dd}" , DateTime.Now);

It prints as: YYYYMMDD

I would like it to print as: YYYY/MM/DD <- Notice the forward slashes.

Can someone point out how I could achieve this?

1
  • Your code works just fine for me as-is. How are you using it? Commented Oct 31, 2013 at 12:19

6 Answers 6

1

try this:

string defTo =DateTime.Now.ToString("yyyy/MM/dd")
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the backslashes:

string defTo = string.Format("{0:yyyy/MM/dd}", DateTime.Now);

Comments

0

try

DateTime.Now.ToString("yyyy/MM/dd")

Comments

0

You were close:

string defTo = string.Format("{0:dd\\/MM\\/yyyy}", DateTime.Now);

EDIT: This should work too:

"{0:dd'/'MM'/'yyyy}"

Comments

0

You don't need a escape character for "/" just remove it:

string defTo = string.Format ( @"{0:yyyy/MM/dd}" , DateTime.Now );

Comments

0

You don't need the forward slashes since you declared it a literal string @"...".

Anything inside the quotes are "as-is" except for other quote characters. To escape them you should double them:

var str1 = @"this is a ""double quote"" in a literal string";

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.