将DateTime转换为没有时间戳的日期 [英] converting DateTime to Date without Time Stamp

查看:133
本文介绍了将DateTime转换为没有时间戳的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var  date =( from  rec  in  dcBusinessAccountingContext.WorkOrdersReportSP_Results  select  rec.Date); 



这里 var date将具有来自WorkOrdersReportSP_Results的所有日期的时间戳,接下来我想将该日期转换为仅在C中没有时间戳的日期#

可以任何人PLZ帮助我







谢谢

Prashanth

解决方案

< blockquote>你可以这样做:

 date = date.Date; 



但如果你显示它,它仍然会给你一个DateTime,时间戳重置为12:00 AM。



如果你只是想在网格中显示DateTime的日期部分,例如,你可以使用ToString()格式化。



 date.ToString( < span class =code-string> d
date.ToString( D





将返回:

 7 / 18/2013 
2013年7月18日星期四





注意:做ToShortDateString()和ToLongDateString()会给你同样的事情


你好,



你的价值是这样的



  var  date =( from  rec   dcBusinessAccountingContext.WorkOrdersReportSP_Results  select  rec.Date); 
date = date.Date;


对不起,



答案是:



var date =(来自于dcBusinessAccountingContext.WorkOrdersReportSP_Results中的rec选择rec.Date).FirstOrDefault();

date = date.GetValueOrDefault( )。日期;



我忘记添加FirstOrDefault()。



当然你必须这样做如果date为null,则为逻辑。


var date = (from rec in dcBusinessAccountingContext.WorkOrdersReportSP_Results select rec.Date);


Here "var date" will have all date's from WorkOrdersReportSP_Results with time stamp, next i want to convert that Date's to only Date without "Time Stamp" in C#
can anybody plz help me



thanks
Prashanth

解决方案

You can do:

date = date.Date;


but if you display it, it will still give you a DateTime with the timestamp reset 12:00 AM.

If you want to just display the date part of a DateTime in a grid for example, you can use the use ToString() formatting.

date.ToString("d")
date.ToString("D")



Which will return:

7/18/2013
Thursday, July 18, 2013



Note: Doing ToShortDateString() and ToLongDateString() will give you the same thing respectively


Hello,

Your value is like this

var date = (from rec in dcBusinessAccountingContext.WorkOrdersReportSP_Results select rec.Date);
date = date.Date;


Sorry,

The answer was:

var date = (from rec in dcBusinessAccountingContext.WorkOrdersReportSP_Results select rec.Date).FirstOrDefault();
date = date.GetValueOrDefault().Date;

I forgot to add FirstOrDefault().

But of course you must do the logic if date is null.


这篇关于将DateTime转换为没有时间戳的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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