.offsetIn()
The .offsetIn()
function is used to obtain the time zone offset at a specific point in time. It provides the difference between the local time and UTC for a given moment.
Syntax
fun Instant.offsetIn(timeZone: TimeZone): UtcOffset
- Instant: A moment in time.
- UtcOffset: An offset from UTC (Coordinated Universal Time).
- TimeZone: provides a conversion between
Instant
andLocalDateTime
values using a collection of rules.
Example
import kotlinx.datetime.TimeZoneimport kotlinx.datetime.Clockimport kotlinx.datetime.offsetInfun main() {val currentTime = Clock.System.now()val timeZone = TimeZone.of("Asia/Kolkata")val offset = currentTime.offsetIn(timeZone)println("Offset from UTC in Kolkata is $offset.")}
The code given above provides the offset for the current time in the Asia/Kolkata
time zone.
Note: The string
Asia/Kolkata
above is a zoneId (returns the time zone identified by the provided zoneId). These IDs are usually in the form ofarea/city
.
The above example will result in the following output:
Offset from UTC in Kolkata is +05:30.
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.