Here is an example. I expected 1900/01/02 but got 1900/1/2 instead. If "1" was an int it would work. Why DOESNT this return 2 di开发者_StackOverflow社区gits? i understand its a string but isnt the point of :00 to specify the digits? Why is it being ignored?
var date = string.Format("{0:0000}/{1:00}/{2:00}", "1900", "1", "2");
Because strings cannot be formatted like numbers; you can, however, specify a width of the target string (but they get padded with spaces, not 0
).
var date = string.Format("{0,4}/{1,2}/{2,2}", "1900", "1", "2");
Why are you trying to format a date from 3 strings, instead of using a DateTime variable?
Then you could format it easily:
DateTime dt = ...;
var dateString = dt.ToString("yyyy/MM/dd");
// yyyy = 4-digit year, MM = 2 digit month, dd = 2 digit day (with leading 0's)
You need to convert to integer before it can be formatted as number:
var date = string.Format("{0:0000}/{1:00}/{2:00}", int.Parse("1900"), int.Parse("1"), int.Parse("2"));
精彩评论