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

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

问题描述

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

  foreach(dt.Rows中的DataRow行)
{
row [StartOn] = Convert.ToDateTime(row [StartOn]。ToString())。ToString(MMM dd)ToString ();
}

如果我运行以下代码,我将获得Aug 09

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

如果我看到这个更改后的行[StartOn]是什么,它包含8 / 9/2016 12:00:00 AM



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

解决方案

StartOn显然是一个DateTime类型。 DateTime类型没有格式。它们是指定年,月,日期和时间的对象(除其他外)。您在转化中所做的所有工作都是剥离时间,以便新的日期时间的上午12:00。


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();
}

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

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

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

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

解决方案

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天全站免登陆