Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I customize my AngularJS application to display local times?

Tags:

angularjs

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'}}" />
like image 360
Alan2 Avatar asked Dec 04 '25 14:12

Alan2


2 Answers

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/

like image 50
Chandermani Avatar answered Dec 06 '25 11:12

Chandermani


Assuming the date you are getting is in UTC, you can use the excellent Moment.js to easily convert it to local time as described here:

http://momentjs.com/docs/#/manipulating/local/

like image 21
Jonathan Schneider Avatar answered Dec 06 '25 13:12

Jonathan Schneider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!