如何在 C# 中执行 Convert.ToDateTime 或解析日期,但在 C# 2.0 中只能获取 dd/mm/yyyy?

发布于 2024-10-03 18:52:46 字数 294 浏览 0 评论 0原文

正如主题所说,我有一个像这样的字符串

string CreatedDate = "12/09/2010"

,我想将它传递给一个只接受它作为 DateTime 的 Web 方法,

customer.dateCreated = Convert.ToDateTime(CreatedDate);

它自然会在 Web 服务不接受的日期之后添加 hh:mm:ss 那么我怎样才能保留当我将其转换为日期时,日期以短格式显示?

提前致谢

As topic says, I have a string like

string CreatedDate = "12/09/2010"

and I want to pass it to a web method that only accepts it as DateTime

customer.dateCreated = Convert.ToDateTime(CreatedDate);

naturally it add hh:mm:ss after the date which the web service doesn't accept so how can I keep the date in the short format when I convert it to a date?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愿得七秒忆 2024-10-10 18:52:46

DateTime 值不知道其格式(实际上它不应该)。如果您的网络服务不接受标准日期/时间格式,那么听起来好像您的网络服务已损坏。 Web服务的实现是怎样的? “customer”是自动生成的代理类吗?

A DateTime value doesn't know anything its formatting (and indeed it shouldn't). It sounds like your web service is broken if it's not accepting standard date/time formatting. What's the implementation of the web service? Is "customer" an autogenerated proxy class?

給妳壹絲溫柔 2024-10-10 18:52:46

假设您有:

void WebMethod(DateTime date);

然后

string dateString = "12/09/2010";

执行下一步:

DateTime date;
if (DateTime.TryParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
    WebMethod(date);
}
else
{
    // raise an error - specified date is not in specified format
}

注意:

date.Hour // 0
date.Minute // 0
date.Seconds // 0

否则,如果您有 DateTime 对象和 WebMethod(string date) 其中日期应采用指定格式,则:

DateTime date = ..;
WebMethod(date.ToString("dd/MM/yyyy"));

Assuming you have:

void WebMethod(DateTime date);

and

string dateString = "12/09/2010";

then do next:

DateTime date;
if (DateTime.TryParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
    WebMethod(date);
}
else
{
    // raise an error - specified date is not in specified format
}

Note:

date.Hour // 0
date.Minute // 0
date.Seconds // 0

Otherwise, if you have DateTime object and WebMethod(string date) where date should be in specified format, then:

DateTime date = ..;
WebMethod(date.ToString("dd/MM/yyyy"));
黑凤梨 2024-10-10 18:52:46

DateTime 有一个 .Date 属性,可以去除时间信息。

DateTime has a .Date property that strips the time information.

南街九尾狐 2024-10-10 18:52:46

WebService 是否仅接受日期“12/09/2010”?通常,网络服务应遵循此处的建议
XML 架构第 2 部分:数据类型第二版

这是 UTC 格式。使用:

DateTime.ParseExact(value, "ddd MMM dd HH:mm:ss zz00 yyyy", null);

大多数情况下都能解决问题。

Does the WebService accept the date as only "12/09/2010"? Typically a webservice should follow the reccomendations here
XML Schema Part 2: Datatypes Second Edition

Which is UTC format. Using:

DateTime.ParseExact(value, "ddd MMM dd HH:mm:ss zz00 yyyy", null);

solves the problem most times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文