.offsetIn()
Published Nov 13, 2023
Contribute to Docs
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.
Contribute to Docs
- 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.
Learn Kotlin on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours