Instant.fromDateTime constructor
Creates a new Instant from the given dateTime
. The Instant will be created in the time zone of the dateTime
unless a timeZoneOffset
is specified. If so, it will be created in the time zone corresponding to that offset from UTC.
Example (local time zone is UTC+02:00):
var instant = Instant.fromDateTime(DateTime(2021, 05, 09, 13, 00), timezoneOffset: 4.0);
print(instant.toIso8601String()); //2021-05-09T17:00:00+04:00
If no timeZoneOffset
is specified, the Instant object is created in the same time zone as the dateTime
object.
Example:
var instantNow = Instant.fromDateTime(DateTime.now().toUtc());
// The timezone offset of instantNow will be 0.0 hour.
Implementation
Instant.fromDateTime(DateTime dateTime, {double? timeZoneOffset})
: timeZoneOffset = (timeZoneOffset != null) ? timeZoneOffset : dateTime.timeZoneOffset.totalHours {
final offsetDateTime =
(timeZoneOffset == null) ? dateTime : dateTime.toUtc().add(Timespan.fromHours(timeZoneOffset));
year = offsetDateTime.year;
month = offsetDateTime.month;
day = offsetDateTime.day;
hour = offsetDateTime.hour;
minute = offsetDateTime.minute;
second = offsetDateTime.second;
}