Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert flutter TimeOfDay to DateTime?

Tags:

flutter

dart

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?

like image 568
Robin Sinha Avatar asked May 06 '18 10:05

Robin Sinha


People also ask

What is TimeOfDay in flutter?

A value representing a time during the day, independent of the date that day might fall on or the time zone. The time is represented by hour and minute pair. Once created, both values cannot be changed. You can create TimeOfDay using the constructor which requires both hour and minute or using DateTime object.

How do you convert 12 hours to 24 hour flutter?

How i Can convert 12 hour time string into 24 hour in flutter? 1. If you have dateTime or TimeOfDay type you can use intl package, otherwise here is my workaround. String time12to24Format (String time) { // var time = "12:01 AM"; int h = int.

How do you get AM or PM from TimeOfDay flutter?

_timeOfDay = await showTimePicker( context: context, initialTime: TimeOfDay. now(), ); DayPeriod dayPeriod = _timeOfDay!. period; print(dayPeriod); // This will return => DayPeriod.am/DayPeriod.pm as per the selection of time. } Save this answer.


1 Answers

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); 
like image 125
Rémi Rousselet Avatar answered Sep 29 '22 21:09

Rémi Rousselet