将 DataRow 中的 DateTime 转换为格式化的日期字符串 [英] Convert DateTime in DataRow to a formatted date string

查看:52
本文介绍了将 DataRow 中的 DateTime 转换为格式化的日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一些我看不清楚的东西,但为了简化,我有下面的代码

I'm hoping that there is something I am not seeing clearly, but to simplify, I have the below code

foreach (DataRow row in dt.Rows)
{
  row["StartOn"] = Convert.ToDateTime(row["StartOn"].ToString()).ToString("MMM dd").ToString();
}

如果我运行下面的代码,我会得到Aug 09"

If I run the below code I get "Aug 09"

Convert.ToDateTime(row["StartOn"].ToString()).ToString("MMM dd").ToString();

如果我在此更改后查看 [StartOn"] 行中的内容,它包含8/9/2016 12:00:00 AM"

If I look to see what is in row["StartOn"] after this change it contains "8/9/2016 12:00:00 AM"

我无法将我的 DataRow 格式化为MMM dd"格式

I'm unable to format my DataRow to an "MMM dd" format

推荐答案

StartOn 显然是 DateTime 类型.DateTime 类型没有格式.它们是指定年、月、日和时间(以及其他内容)的对象.您在转换中所做的只是去除时间,以便新日期时间的时间为上午 12:00.

StartOn is apparently a DateTime type. DateTime types do NOT have a format. They are an object that specifies year, month, date, and time (among other things). All you are doing in your conversions is stripping out the time so that the new datetime has a time of 12:00 am.

这篇关于将 DataRow 中的 DateTime 转换为格式化的日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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