Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 820 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert flutter TimeOfDay to DateTime?

#1
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?

Reply

#2
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));

```
Reply

#3
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"
Reply

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

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

#5
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.

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

#6
You can set an extension on the `TimeOfDay` class:

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

then use it like this:

```dart
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through