Dart DateTime isUtc
Syntax & Examples
Syntax of DateTime.isUtc
The syntax of DateTime.isUtc property is:
bool isUtc
This isUtc property of DateTime true if this DateTime
is set to UTC time.
Return Type
DateTime.isUtc returns value of type bool
.
✐ Examples
1 Check if the current DateTime is in UTC
In this example,
- We create a DateTime object
dt
using theDateTime.now()
constructor, which represents the current date and time. - We then access the
isUtc
property ofdt
to determine if it is set to UTC time. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.now();
print(dt.isUtc);
}
Output
Output varies depending on whether the current date and time is set to UTC.
2 Check if a specific DateTime is in UTC
In this example,
- We create a DateTime object
dt
using theDateTime.utc()
constructor, specifying the year, month, and day. - We then access the
isUtc
property ofdt
to determine if it is set to UTC time. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.utc(2024, 5, 3);
print(dt.isUtc);
}
Output
true
3 Check if a parsed DateTime is in UTC
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
isUtc
property ofdt
to determine if it is set to UTC time. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.parse('2024-05-03T10:15:30');
print(dt.isUtc);
}
Output
false
Summary
In this Dart tutorial, we learned about isUtc property of DateTime: the syntax and few working examples with output and detailed explanation for each example.