如何更改 DateTime 格式以适合我构建的字符串?

How can i change the DateTime format to fit the string i build?

我有这个循环 :

for (int i = 0; i < dateTime.Count; i++)
{
    link = "test" + dateTime[i].Year + dateTime[i].Month
              + dateTime[i].Day + dateTime[i].TimeOfDay.Hours
              + dateTime[i].TimeOfDay.Minutes
              + "text1" + infraredorvisual;
    WebClient client1 = new WebClient();
    string filePath = Path.Combine(satimagesdir, "SatImage" + i + ".GIF");

    client1.DownloadFile(link, filePath);
    client1.Dispose();
}

在列表 dateTime 中,我有一些项目,第一个看起来像这样:[0] = {12/01/2015 08:00:00}

我构建的 link 变量的字符串是:

texthttp://www.sat24.com/image2.ashx?region=is&time=201511280&text1

但是时间格式不对
我应该进入 link 这个字符串:

http://www.sat24.com/image2.ashx?region=is&time=201501120330&ir=True

两个字符串都只是示例。

第一个时间是 9 位数字:年 = 2015 然后是第 1/12 天和时间 80 时间 80 表示八。

但是日期和时间的格式应该像第二个例子中的那样:

year = 2015 day = 01/12 and time 0330 .... 201501120330

如何像第二个示例那样使用正确的时间和日期格式构建 link 变量?

您可以使用 DateTime.ToString:

string result = dateTime[i].ToString("yyyyddMMHHmm");

http://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx