Dart DateTime timeZoneOffset
Syntax & Examples
Syntax of DateTime.timeZoneOffset
The syntax of DateTime.timeZoneOffset property is:
Duration timeZoneOffset
This timeZoneOffset property of DateTime the time zone offset, which is the difference between local time and UTC.
Return Type
DateTime.timeZoneOffset returns value of type Duration
.
✐ Examples
1 Get the time zone offset of the current DateTime
In this example,
- We create a DateTime object
dt
using theDateTime.now()
constructor, which represents the current date and time. - We then access the
timeZoneOffset
property ofdt
to get the time zone offset. - We print the result to standard output. Output varies depending on the current date and time.
Dart Program
void main() {
var dt = DateTime.now();
print(dt.timeZoneOffset);
}
Output
5:30:00.000000 Output varies depending on the current date and time.
2 Get the time zone offset of a specific DateTime in UTC
In this example,
- We create a DateTime object
dt
using theDateTime.utc()
constructor, specifying the year, month, day, hour, minute, and second. - We then access the
timeZoneOffset
property ofdt
to get the time zone offset. - We print the result to standard output. Output varies depending on the current date and time.
Dart Program
void main() {
var dt = DateTime.utc(2024, 5, 3, 10, 15, 30);
print(dt.timeZoneOffset);
}
Output
0:00:00.000000
3 Get the time zone offset of a parsed DateTime
In this example,
- We create a DateTime object
dt
by parsing a date-time string '2024-05-03T10:15:30' using theDateTime.parse()
method. - We then access the
timeZoneOffset
property ofdt
to get the time zone offset. - We print the result to standard output. Output varies depending on the current date and time.
Dart Program
void main() {
var dt = DateTime.parse('2024-05-03T10:15:30');
print(dt.timeZoneOffset);
}
Output
5:30:00.000000 Output varies depending on the parsed date and time.
Summary
In this Dart tutorial, we learned about timeZoneOffset property of DateTime: the syntax and few working examples with output and detailed explanation for each example.