我在c#中使用web api构建了一个Web服务。
我创建了一个从数据库中检索某些记录的方法。其中一列是datetime。我想格式化它。
所以我有这个方法从数据库中检索记录:
[NonAction]
private IQueryable<WSOmniacare.Models.AAHome.ImmaginiSecSoc.ImmaginiSecSocDTO> getSecSocImages(int? id)
{
var strItem = from u in db_data.CAMERA_SEC_SOC
where u.ID == id
select u.Image;
String imageBas64 = GetString(strItem.First());
if (id != null)
{
return from u in db_data.CAMERA_SEC_SOC
where u.ID == id
select new WSOmniacare.Models.AAHome.ImmaginiSecSoc.ImmaginiSecSocDTO()
{
image = imageBas64,
image_width = u.image_width,
image_height= u.image_height,
type = u.type,
timestamp =u.timestamp.ToString(),
dateTime = u.timestamp,
rectangle = new WSOmniacare.Models.AAHome.ImmaginiSecSoc.ImmaginiSecSocDTO.Rectangle()
{
rects = from pi in db_data.CAMERA_SEC_SOC_Rectangles
where pi.ID_SecSoc == id
select new WSOmniacare.Models.AAHome.ImmaginiSecSoc.ImmaginiSecSocDTO.Rectangle.Rect()
{
height= pi.height,
width = pi.width,
x = pi.x,
y=pi.y
}
}
};
}
return null;
}
这是创建回复的方法:
public HttpResponseMessage getSecSocData(int? id = null)
{
try
{
IQueryable<ImmaginiSecSoc.ImmaginiSecSocDTO> lista = getSecSocImages(id);
List<ImmaginiSecSoc.ImmaginiSecSocDTO> listaModificata = new List<ImmaginiSecSoc.ImmaginiSecSocDTO>();
foreach (ImmaginiSecSoc.ImmaginiSecSocDTO a in lista)
{
a.timestamp = a.dateTime.ToString("MM/dd/yyyy HH:mm:ss.fff");
listaModificata.Add(a);
}
return Request.CreateResponse(HttpStatusCode.OK, new RCamera((short)status_code.Success, "Ok", listaModificata));
}
catch (Exception e)
{
e = e.GetBaseException();
log.Error(string.Format("{0} {1}", e.Message, e.StackTrace));
return Request.CreateResponse(HttpStatusCode.InternalServerError, new RMessage((short)status_code.Exception, HttpStatusCode.InternalServerError.ToString()));
}
}
如您所见,我必须循环列表并格式化字段时间戳。 现在我的问题是,如何将解析JSON的代码更改为格式化我的日期?
答案 0 :(得分:0)
你已经在做
了timestamp =u.timestamp.ToString(),
在getSecSocImages
方法中,
如果格式始终相同,请将其更改为
timestamp =u.timestamp.ToString("MM/dd/yyyy HH:mm:ss.fff"),