C# 如何传递 DateTime 值参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17646088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:05:13  来源:igfitidea点击:

How to pass DateTime value parameter?

c#datetime

提问by deva

I have a function that have parameter for DateTime

我有一个具有参数的函数 DateTime

   AddNewRowToTable(....,DateTime ExpDate)

when I call that method like this below:

当我像下面这样调用该方法时:

   AddNewRowToTable(....,"2008/04/14")

It said it can't convert stringto DateTime.

它说它不能转换stringDateTime.

help!

帮助!

采纳答案by TGH

You have to do AddNewRow(....,new DateTime(..))

or AddNewRow(....,DateTime.ParseExact(dateString, format, provider))

或者 AddNewRow(....,DateTime.ParseExact(dateString, format, provider))

There is no implicit conversion from string

没有从字符串的隐式转换

回答by Shaharyar

You will have to convert the stringto type DateTime. You can convert it in this way:

您必须将 转换string为 type DateTime。您可以通过以下方式转换它:

AddNewRow(....,new DateTime.ParseExact("2009-05-08", "yyyy-MM-dd",
                                       System.Globalization.CultureInfo.InvariantCulture))

回答by Jay Patel

You need to pass correct type for the input parameter. Look into function overloading if you want to pass a Stringand then convert it into DateTimeinside the function.

您需要为输入参数传递正确的类型。如果要传递 aString然后将其转换DateTime为函数内部,请查看函数重载。

回答by Tanya Fomenko

To add a little to the previous answers new DateTime() it appears to expect a long.

为之前的答案添加一点 new DateTime() 似乎需要很长时间。

So it has to be divided by "," as such:

所以它必须被“,”除以这样:

AddNewRowToTable(....,new DateTime(2008,04,14))