Robin Sinha
Robin Sinha

Reputation: 1192

How to convert flutter TimeOfDay to DateTime?

I have a time picker that returns the TimeOfDay object. But I have to save that value in a database as an integer of milliseconds which obtained from DateTime.millisecondsSinceEpoch. How can I achieve that?

Upvotes: 52

Views: 44346

Answers (5)

Adnan
Adnan

Reputation: 1293

You can set an extension on the TimeOfDay class:

extension TOD on TimeOfDay {
  DateTime toDateTime() {
    return DateTime(1, 1, 1, hour, minute);
  }
}

then use it like this:

TimeOfDay timeOfDay = TimeOfDay.now();

DateTime dateTimeOfDay = timeOfDay.toDateTime();

if you define the extenson in another file and you have not imported it yet, if you are using vsc you can just type timeOfDay.toDateTime(); then ctrl + . and vsc will suggest importing that file. if you're not using vsc, then I guess you have to import the file of the extension manually.

Upvotes: 1

Rémi Rousselet
Rémi Rousselet

Reputation: 277137

This is not possible. TimeOfDay holds only the hour and minute. While a DateTime also has day/month/year

If you want to convert it, you will need more information. Such as the current DateTime. And then merge the two into one final datetime.

TimeOfDay t;
final now = new DateTime.now();
return new DateTime(now.year, now.month, now.day, t.hour, t.minute);

Upvotes: 102

Roc Boronat
Roc Boronat

Reputation: 12151

Here's just a tiny method to join a DateTime and a HourOfDay:

DateTime join(DateTime date, TimeOfDay time) {
  return new DateTime(date.year, date.month, date.day, time.hour, time.minute);
}

Upvotes: 1

Adam Smaka
Adam Smaka

Reputation: 6393

You can use DateTime extension

extension DateTimeExtension on DateTime {
  DateTime applied(TimeOfDay time) {
    return DateTime(year, month, day, time.hour, time.minute);
  }
}

then you can use it like this:

final dateTime = yourDate.applied(yourTimeOfDayValue);

and change your sdk version in pubspec.yaml to

environment:
 sdk: ">=2.7.0 <3.0.0"

Upvotes: 20

Code Runner
Code Runner

Reputation: 1028

You can use entension like this and also can add some more extension methods in separate file like dateTime_extensions.dart to make your work easy for future projects as well

file: dateTime_extensions.dart;


extension DateTimeExtension on DateTime {
DateTime setTimeOfDay(TimeOfDay time) {
    return DateTime(this.year, this.month, this.day, time.hour, time.minute);
  }

  DateTime setTime(
      {int hours = 0,
      int minutes = 0,
      int seconds = 0,
      int milliSeconds = 0,
      int microSeconds = 0}) {
    return DateTime(this.year, this.month, this.day, hours, minutes, seconds,
        milliSeconds, microSeconds);
  }

  DateTime clearTime() {
    return DateTime(this.year, this.month, this.day, 0, 0, 0, 0, 0);
  }

   ///..... add more methods/properties for your convenience 
}

use it like this


import 'package:your_app/dateTime_extensions.dart';

    date.clearTime(); //to clear timeSpan
    date.setTime(); //also be used to clear time if you don't provide any parameters
    date.setTime(hours: 16,minutes: 23,seconds: 24); // will set time to existing date eg. existing_date 16:23:24
    date.setTimeOfDay(TimeOfDay(hour: 16, minute: 59));

Upvotes: 6

Related Questions