2016-08-01 83 views

回答

3

只需创建一个DateTime(),然后使用.ToString过载,该过载可以指定一种格式:

new DateTime(year, month, 1).ToString("MM/yyyy"); 

阅读MSDN上的different formatting选项

1

您需要使用日期用它来创建日期时间

int month = 03; 
int year = 2006; 

DateTime dt = new DateTime(year, month, 1); 

那么你可以把它作为mm/yyyy格式字符串

string s = dt.ToString("MM/yyyy") 
1

要形成一个String您可以尝试string interpolation(可从c#6.0获得),例如。

int month = 3; 
    int year = 2006; 

    string s = $"{month:00}/{year}"; 

如果您想DateTime,不String

DateTime dt = new DateTime(year, month, 1); 

为了表示DateTimemm/yyyy格式创建“:

string s = $"{dt:MM/yyyy}";