简体   繁体   English

Grails - 考虑时区的用户显示日期

[英]Grails - display date by taking into account Time Zone's user

I know I have to set the TimeZone somewhere but I don't know how can I "get" the current TimeZone from the user and display (dynamically?) the date with the correct offset. 我知道我必须在某个地方设置TimeZone,但我不知道如何从用户“获取”当前的TimeZone并使用正确的偏移量显示(动态?)日期。

This is my code: 这是我的代码:

Domain: 域:

class MyClass {

  Date myDate

}

Controller: 控制器:

def unixSeconds = 1386760029
Date date = new Date(unixSeconds*1000L)

GSP: GSP:

<g:formatDate format="yyyy-MM-dd HH:mm" date="${MyClassInstance.myDate}" timeZone="${TimeZone.getTimeZone("GMT")}"/>

You can't have the user timezone because this information is not sent in the request. 您不能拥有用户时区,因为此信息不会在请求中发送。

You could try to get the user location using Geolocation or GeoIP to retrieve the user country and then set the corresponding timezone. 您可以尝试使用GeolocationGeoIP获取用户位置以检索用户所在国家/地区,然后设置相应的时区。

Another way is to get the timezone difference between UTC and Local Time using JavaScript getTimezoneOffset() Method : 另一种方法是使用JavaScript getTimezoneOffset()方法获得UTC和本地时间之间的时区差异:

var d = new Date()
var n = d.getTimezoneOffset();

But it's easier to let the user setup its timezone manually. 但是让用户手动设置其时区更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM