我正在为我的应用程序使用ASP.NET MVC / ASP.NET Web API后端。 当用户更新数据时,时间记录如下:
public HttpResponseMessage PutContent(int id, Content content) { if (id != content.ContentId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } try { content.ModifiedDate = DateTime.Now; content.ModifiedBy = User.Identity.GetUserId(); _uow.Contents.Update(content); _uow.Commit(); return Request.CreateResponse(HttpStatusCode.OK, content); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }当用户查看修改时间时,他们会看到我假设的是服务器时间。 有没有办法可以让他们看到改变的当地时间?
请注意,我已经做了一些格式化但是我不确定是否有一种方法可以转换日期并使其与可能位于任何位置的本地用户相匹配:
<input disabled="disabled" type="text" value="{{modal.data.modifiedDate | date:'MM/dd/yy HH:mm'}}" />I am using an ASP.NET MVC / ASP.NET Web API back-end for my application. When a user updates data the time is recorded like this:
public HttpResponseMessage PutContent(int id, Content content) { if (id != content.ContentId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } try { content.ModifiedDate = DateTime.Now; content.ModifiedBy = User.Identity.GetUserId(); _uow.Contents.Update(content); _uow.Commit(); return Request.CreateResponse(HttpStatusCode.OK, content); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }When the user looks at the modified time they see what I assume is the server time. Is there a way that I can allow them to see the local time that the change was made?
Note that already I do some formatting but I am not sure if there is a way that I can convert the date and have this matched up with my local users who could be in any location:
<input disabled="disabled" type="text" value="{{modal.data.modifiedDate | date:'MM/dd/yy HH:mm'}}" />最满意答案
首先,最好以UTC格式保存服务器上的日期。 所以在服务器上使用DateTime.UtcNow方法。
在将数据发送到客户端时,如果您没有自定义格式化日期,我相信发送日期也包含时区信息。 AngularJS可以使用date过滤器来处理正确的问题。 看到这个小提琴http://jsfiddle.net/jHSLe/1/
To start with it is always better to save the dates on server in UTC. So on the server use the method DateTime.UtcNow.
And while sending the data to client, if you are not custom formatting the date, i believe the date send contains the timezone information as well. AngularJS can handle that correct using date filter. See this fiddle http://jsfiddle.net/jHSLe/1/
更多推荐
发布评论