This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
unable to convert string date in Format yyyyMMddHHmmss to DateTime dart
(11 answers)
Closed last year.
Hi i have an array contains
List<dynamic> am = ['09:00', '09:30', '10:00', '10:30', '11:00', '11:30'];
and if they are selected
String time = '09:00'
how can I change time to the DateTime value or TimeOfDay
import 'package:intl/intl.dart';
if your time format is fixed (for example "hours:minutes" ) you can use this method
DateTime? _convertStringToDateTime(String time){
DateTime? _dateTime;
try{
_dateTime = DateFormat("hh:mm").parse(time);
}catch(e){}
return _dateTime;
}
You can use this:
TimeOfDay toTimeOfDay(String time){
List<String> timeSplit = time.split(":");
int hour = int.parse(timeSplit.first);
int minute = int.parse(timeSplit.last);
return TimeOfDay(hour: hour, minute: minute);
}
https://pub.dev/packages/date_format
See this package, By means of this package you can format your DateTime.
Related
Guys. I want to write a simple code to add X weeks to a chosen Date but I just can't figure out how. I always get a ton of errors. The function should basically return the chosen date plus "addedweeks" in the format "dd-MM-yyyy". Below is my last attempt. Thanks in advance!
import 'dart:math' as math;
String? addXWeeks(
DateTime? datum,
int? addedweeks,
) {
// add 2 weeks
DateTime date = datum.toLocal;
date = DateTime(date.year, date.month, date.day + (addedweeks*7));
}
this is very easy. DateTime has a method called add. You only need to add 7 days, because duration has not the propertie "weeks".
Here is a samle:
void main() {
// Current date
DateTime date = DateTime.now();
// Weeks you want to add
int weeksToAdd = 10;
print("Before: $date");
// Multiply 7 (days a week) with your week and set int to and int
date = date.add(Duration(days: (7 * weeksToAdd).toInt()));
print("After: $date");
// Format date
print("Format date: ${date.day}.${date.month}.${date.year}");
}
The output:
Before: 2022-10-06 13:23:16.342888
After: 2022-12-15 12:23:16.342888
Format date: 15.12.2022
using DateTime we can use the following to get the difference
DateTime myFirstDateTime = DateTime.now();
DateTime mySecondtDateTime = DateTime.now();
myFirstDateTime.difference(mySecondtDateTime).inMinutes // or inDay
the previous code is working as expected but how can I handle the same between Timestamp and DateTime ?
I used the following but it give wrong result and strange negative numbers like -5623
Timestamp myTimestamp = // here I get the value from my Firebase field
DateTime myDateTime = DateTime.now();
then I convert it `toDate()`
myDateTime.difference(myTimestamp.toDate()).inMinutes; //or inDay
//output strange value
How can I do it? Thanks
You might have to move the brackets as follows:
myDateTime.difference(myTimestamp.toDate()).inMinutes;
Try this one out
var myTimestamp = // here you get the value from your Firebase field
DateTime myDateTime = DateTime.now();
DateTime myFirebaseTime = DateTime.parse(myTimestamp.toDate().toString());
myDateTime.difference(myFirebaseTime).inDays;
I will receive DateTime in UTC format from API and I need to convert the DateTime to local time zone based on the condition.
We have toLocal() method to change the time based on the device time zone.
condition: 23-4-2021 // no need to change it to toLocal()
23-4-2021 00:00:00 // no need to change it to toLocal()
23-4-2021 10:30:34 // need to change it to toLocal()
If we have time in the DateTime then only we have to change it in local time.
DateTime utcToDateTimeLocal(DateTime value) {
return value.toLocal();
}
Thanks!
Something like this maybe ?
DateTime utcToDateTimeLocal(DateTime value) {
if (value.hour!=0 || value.minute!=0 || value.second!=0){
return value.toLocal();
}
return value;
}
Here is quick solution that work for me.
You can get time (or any format) by DateFormat class
In your case
dateTime = '23-4-2021 10:30:34'
final format = DateFormat('HH:mm a');
final clockString = format.format(dateTime);
you will get // 10:30 AM
DateTime utcToDateTimeLocal(DateTime value) {
var dateFormat =
DateFormat("dd-mm-yyyy hh:mm a"); // you can change the format here
var utcDate =
dateFormat.format(DateTime.parse(value.toString())); // pass the UTC time here
var localDate = dateFormat.parse(utcDate, true).toLocal().toString();
return DateTime.parse(localDate);
}
I am getting Timestamp from Firebase Firestore and then formatting it into date and then want to take out the difference in days. And for that I am doing this:-
DateTime myDateTime = DateTime.parse(nameTimestamp!=null? nameTimestamp.toDate().toString() :DateTime.now().toString());
print('$myDateTime');
String formattedDateTime = DateFormat('yyyy,MM,dd').format(myDateTime);
print('$formattedDateTime');
final nameChangeDate = DateTime(2021,6,1);
final todayDate = DateTime.now();
final difference = todayDate.difference(nameChangeDate).inDays;
print(difference);
And now the problem I am facing is if I put formattedDateTime in front of nameChangeDate like mentioned below then at difference there's appear an error that The argument type 'String' can't be assigned to the parameter type 'DateTime'.
final nameChangeDate = formattedDateTime;
final todayDate = DateTime.now();
final difference = todayDate.difference(nameChangeDate).inDays; ///Error
print(difference);
Help me in this or guide me to take out the difference in some other way.
I am trying to use a PageView.builder in my application and i wanted each page to display a particular date. I have defined a list of type DateTime : List<DateTime> _month;. how do i store all the days in a particular month (30 days) in the list _month?
The stored dates must be of type DateTime in order to implement this in my application.
This code will do what you need. I don't know the entire structure of your code so I just wrote this example!
void main() {
int month = 1;
DateTime start = DateTime(2019,month);
DateTime end = DateTime(2019,month+1);
int c = (end.toUtc().difference(start.toUtc()).inDays);
List<DateTime> _month = [];
_month.addAll(List.generate(c,(index) => start.toUtc().add(Duration(days:index)).toLocal()));
print(_month);
}