将特定的DateTime日期作为输入参数调用,c#

时间:2013-07-30 09:59:24

标签: c# datetime

如何使用特定日期作为输入值?

var experiment1 = WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);

稍后,我会将它转换为字符串,但我需要将其作为DateTime输入。

public static string GetDisplayString(string city, DateTime date, double temp)
{         
}

我使用DateTime.Today作为占位符,只是因为它有效。问题是,我需要将其作为一年中某个月的特定日期输入。 (我尝试过使用(10,10,10),但它只是给我一个编译器错误。

编辑:我无法相信我没有想到我需要做的就是添加“新”。谢谢大家

2 个答案:

答案 0 :(得分:0)

(10, 10, 10)你想要年份10吗?

使用包含一年,一个月和一天的构造函数。如下所示。

DateTime dt = new DateTime(2013, 12, 12);

然后调用你的方法

var experiment1 = WorkingWithDates.GetDisplayString("London", dt, 45.00);

PS:如果你想要10年级

DateTime dt = new DateTime(10, 10, 10);

会奏效。年份为0010

答案 1 :(得分:0)

WorkingWithDates.GetDisplayString("London", DateTime.Today, 45.00);

您需要使用DateTime对象代替DateTime.Today,因此请使用

构建您的实例
DateTime newdate = new DateTime(2013,10,10) // year, month , day

然后

WorkingWithDates.GetDisplayString("London", newdate, 45.00);