我可以自定义AngularJS应用程序以显示当地时间吗?(Can I customize my AngularJS application to display local times?)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
我可以自定义AngularJS应用程序以显示当地时间吗?(Can I customize my AngularJS application to display local times?)

我正在为我的应用程序使用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/

更多推荐

本文发布于:2023-04-24 12:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/87beb09e20929e44eb81851511065cf3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   当地时间   应用程序   AngularJS   customize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!