Displaying date/time in user's timezone - on client side

后端 未结 3 801
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 07:31

I have a web application that displays datetime stamps on every page, for example:

December 12, 2009 6:00 pm

I would like to dyn

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 08:02

    You can use Date.getTimeZoneOffset() to get the local offset from GMT.

    var date = new Date();
    var offset = date.getTimezoneOffset();
    

    From there its a SMOP. Using the Javascript Date object you can call date.toDateString to get a human readable date. If you need it server side, have the Javascript send the offset to your server with a GET or POST.

    Formatting the date is another story. Javascript doesn't provide much built in. You could hack it up yourself, but that way bugs and madness lies. Date formatting is something any good Javascript library should provide. For example, JQuery has Datepicker.formatDate.

提交回复
热议问题