在datatable列中更改DATETIME格式,而不使用C#中的循环 [英] Changing DATETIME format in datatable column without using loop in C#

查看:341
本文介绍了在datatable列中更改DATETIME格式,而不使用C#中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,其中包含subscribeDate列,日期以MM / dd / yyyy hh:mm:ss格式显示。我想将此列中的所有日期更改为MM-dd-yyyy hh:mm:ss格式。有没有办法可以在不运行循环的情况下执行?

I have a Data Table that has a column 'subscribeDate' that has dates in MM/dd/yyyy hh:mm:ss format. I would like to change all the dates in this column to MM-dd-yyyy hh:mm:ss format. Is there a way that I can do it without running a loop?

推荐答案

我希望 DataTable 的类型为$ code> DateTime 或 DateTimeOffset
假设是这种情况,他们不具有格式。他们只是日期和时间。如何将它们转换为字符串取决于您。

I would hope that the values in the DataTable are of type DateTime or DateTimeOffset. Assuming that's the case, they don't have a format. They're just dates and times. How you convert them to a string is up to you.

如果他们不是已经在适当的数据类型中,那么您应该尝试更改如何获取数据开始。尽可能早地将它从字符串格式转换为更合适的数据类型,并且只有当您真正需要时才将返回到字符串。

If they're not already in an appropriate data type, you should try to change how you obtain the data to start with. Convert it from a string format to a more appropriate data type as early as you possibly can, and only convert it back to a string when you really need to.

您尚未解释如何显示 DataTable ,但是我知道的大多数方式允许您指定格式字符串 - 那是,您可以在其中指定您的 MM-dd-yyyy HH:mm:ss 格式。 (注意 HH 而不是 hh ,以获得24小时。)

You haven't explained how you're displaying the DataTable, but most ways that I'm aware of allow you to specify a format string - that's where you would specify your MM-dd-yyyy HH:mm:ss format. (Note HH rather than hh, to get 24 hours.)

这篇关于在datatable列中更改DATETIME格式,而不使用C#中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆