How to elegantly deal with timezones

后端 未结 7 495
庸人自扰
庸人自扰 2020-11-28 17:31

I have a website that is hosted in a different timezone than the users using the application. In addition to this, users can have a specific timezone. I was wondering how ot

相关标签:
7条回答
  • 2020-11-28 18:26

    For output, create an display/editor template like this

    @inherits System.Web.Mvc.WebViewPage<System.DateTime>
    @Html.Label(Model.ToLocalTime().ToLongTimeString()))
    

    You can bind them based on attributes on your model if you want only certain models to use those templates.

    See here and here for more details on creating custom editor templates.

    Alternatively, since you want it to work for both input and output, I would suggest extending a control or even creating your own. That way you can intercept both the input and outputs and convert the text/value as needed.

    This link will hopefully push you in the right direction if you want to go down that path.

    Either way, if you want an elegant solution, its going to be a bit of work. On the bright side, once you have done it once you can keep it in your code library for future use!

    0 讨论(0)
提交回复
热议问题