If you already have it as a DateTime
, use:
string x = dt.ToString("yyyy-MM-dd");
See the MSDN documentation for more details. You can specify CultureInfo.InvariantCulture
to enforce the use of Western digits etc. This is more important if you're using MMM for the month name and similar things, but it wouldn't be a bad idea to make it explicit:
string x = dt.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
If you have a string to start with, you'll need to parse it and then reformat... of course, that means you need to know the format of the original string.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…