[英]How to get only date from date time using Asp. Net c#?
In sql database, value is in this format 2016-07-16
and it's datatype is date
.在 sql 数据库中,值的格式为
2016-07-16
,数据类型为date
。
But when i am getting it gives me like that: 7/16/16 12:00:00 AM
但是当我得到它时,它给了我这样的信息:
7/16/16 12:00:00 AM
I want to get only the date 2016-07-16
but with this format 16/07/2016
.我只想获取日期
2016-07-16
但格式为16/07/2016
。
How to get it?如何获得?
When you fetch the data from the sql you should manipulate the data and change this value. 从sql提取数据时,应处理数据并更改此值。 i don't know if your using linq or using EF.
我不知道您是使用linq还是使用EF。
if you use linq 如果您使用linq
var query = from p in db.table
select new view{
CreatedOn = p.CreatedOn.ToString("dd/MM/yyyy")
};
but if you use EF and code first you can set the GET method on its property like this : 但是,如果您先使用EF并进行编码,则可以像这样设置GET方法的属性:
private DateTime _createdOn;
public string CreatedOn {
get {
return _createdOn.ToString("dd/MM/yyyy");
}
set{
_createdOn = value;
}
}
public string GetDateOnly(string DateString) { 公共字符串GetDateOnly(字符串DateString){
DateTime _retVal = Convert.ToDateTime(DateString);
return _retVal.ToString('dd/MM/yyyy');
} }
this this method where by passing datetime string 此方法通过传递日期时间字符串
I had the same problem.我有同样的问题。 This is what I did following @M.R.Safari using the Entity Framework migrations and code first approach.
这是我在@M.R.Safari 之后使用实体框架迁移和代码优先方法所做的。
[Required]
public DateTime StartDate { get; set; }
public string StartDateShort
{
get
{
return StartDate.ToString("dd/MM/yyyy");
}
set
{
StartDateShort = value;
}
}
[Required]
public DateTime EndDate { get; set; }
public string EndDateShort
{
get
{
return EndDate.ToString("dd/MM/yyyy");
}
set
{
EndDateShort = value;
}
}
I did get an error on the Set method using @M.R.Safari example since it was trying to overwrite the StartDate(DateTime) value to a String, so I instead used the Set method for the String version.我确实在使用 @M.R.Safari 示例的 Set 方法上遇到错误,因为它试图将 StartDate(DateTime) 值覆盖为字符串,因此我改为使用 Set 方法作为字符串版本。 I edit only the StartDate(DateTime) value in my forms and the string value gets updated automatically.
我只编辑 forms 中的 StartDate(DateTime) 值,字符串值会自动更新。 I use the string value for views.
我将字符串值用于视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.