简体   繁体   中英

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.

This is my code:

Domain:

class MyClass {

  Date myDate

}

Controller:

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

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.

Another way is to get the timezone difference between UTC and Local Time using JavaScript getTimezoneOffset() Method :

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

But it's easier to let the user setup its timezone manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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