Reputation: 3302
I want to merge these two date and time variables and have another variable called var toUTC
.
I have two variables for date and time from the Date Picker
and Time Picker
.
var selectedDate;
var selectedTime;
print(selectedDate); // 2021-02-06 00:00:00.000
print(selectedTime); // 1:15:00.000000
The var selectedDate
has the time but I don't want to use this. I want to replace it with the value from var selectedTime
.
What I am trying to do is
print(toUTC) // 2021-02-06 1:15:00.00000 <---- merge selectedDate and selectedTime
to convert to UTC
time.
I've tried to take only 2021-02-06
and merge with selectedTime
, but I failed to convert to UTC
.
Upvotes: 1
Views: 3640
Reputation: 199
To build on JayDev's answer, you could also create an extension method that directly accepts a TimeOfDay.
extension DateTimeExtension on DateTime {
DateTime at(TimeOfDay time) {
return copyWith(hour: time.hour, minute: time.minute);
}
}
And use it like so:
final selectedDateTime = selectedDate.at(selectedTime);
Upvotes: 0
Reputation: 124
///maps[0]['TimeInTime'] = 12:25:55
var arr = maps[0]['TimeInTime']!.split(':');
var TodayFrom = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().subtract(Duration(days: 1)).day, int.parse(arr[0]), int.parse(arr[1]),00,00);
Upvotes: 0
Reputation: 1360
final toUTC = DateTime(selectedDate.year, selectedDate.month, selectedDate.year,
selectedTime.hour, selectedTime.minute);
Or if its something you are going to need to do regularly you could make an extension for it. The below does it by passing the hour and minute in as separate variables, but you could change that to be another DateTime value if you would prefer.
extension DateTimeExtensions on DateTime {
/// Creates a new date time with the given date but with the time
/// specified from [time]
DateTime withTime([int hour = 0, int minute = 0]) =>
DateTime(this.year, this.month, this.day, hour, minute);
}
To convert to UTC there is the method toUtc() on the DateTime class. I'm not sure if you've just not spotted that or if there is an issue I'm not seeing?
Upvotes: 7