Invalid date format for validation flutter - flutter

I have a form, the date picker is supposed to validate and check whether the end date is later than the start date. But my coding got error which says
Invalid date format
09/01/2023`
Here is my validation code:
TextEditingController _sDate = TextEditingController();
TextEditingController _eDate = TextEditingController();
String? endDateValidator(value) {
DateTime eDate = DateTime.parse(_eDate.text);
DateTime sDate = DateTime.parse(_sDate.text);
eDate = DateFormat("yyyy-MM-dd").format(eDate);
if (_sDate != null && _eDate == null) {
return "select Both data";
}
if (_eDate == null) return "select end date";
if (_eDate != null) {
if(eDate.isBefore(sDate))
return "End date must be after start date";
}
return null;}
How do I fix this?

Your date format is not regular(yyyy-MM-dd) format, you need to use custom format to format it, so instead of this:
DateTime eDate = DateTime.parse(_eDate.text);
DateTime sDate = DateTime.parse(_sDate.text);
eDate = DateFormat("yyyy-MM-dd").format(eDate);
You can use intl package and do this::
DateTime eDate = DateFormat("dd/MM/yyyy").parse(_eDate.text);
also in line four of your code, format(eDate) return you a string, you can't pass it to eDate, because it is DateTime. Also you don't need this line at all.

There are two methods of formatting dates if we are working in Flutter:
1.- Add the intl package (https://pub.dev/packages/intl) and you can access to this functions:
DateTime now = DateTime.now();
String formattedDate = DateFormat.yMMMEd().format(now);
print(formattedDate);
Output:
Tue, Jan 25, 2022
2.- Using Custom Pattern
DateTime now = DateTime.now();
formattedDate = DateFormat('EEEE, MMM d, yyyy').format(now);
print(formattedDate);
Output:
Tuesday, Jan 25, 2022
For more information:
https://api.flutter.dev/flutter/intl/DateFormat-class.html
I hope it was useful for you.

Related

Dart Flutter adding weeks to a DateTime

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

How to get difference between 1 Timestamp and 1 dateTime in minute or day

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;

How can we identify time section in datetime flutter

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);
}

How to extract day, month, and year from date returned by QML Calender?

The Calendar clicked signal returns a date as follows:
2015-11-13T00:00:00
However, I would like to have a date formatted like this:
Fri Nov 13 2015
This is what I tried:
onSelectedDateChanged:
{
calender.visible = false;
selectedDate = selectedDate.toLocaleTimeString(Qt.LocalDate, Locale.ShortFormat);
textOfSelectedDate.text = Date.fromLocaleTimeString(Qt.LocalDate, selectedDate, Locale.ShortFormat)}
}
textOfSelectedDate is the id of the text box where this date will be displayed.
How can I extract day, month, and year in a desired format from Date returned by Calender?
QML's date type extends Javascript's Date. Thus you can do:
onSelectedDateChanged: {
const day = selectedDate.getDate();
const month = selectedDate.getMonth() + 1; //assuming you want 1..12, getMonth()'s return value is zero-based!
const year = selectedDate.getFullYear();
...
}
First of all, date is similar to JS date type. So you can use all its functions, like getDate() etc. See it here
Also, you can use Qt.formatDate() object to format the result. In your case it can be as follows:
onClicked: {
console.log(Qt.formatDate(date,"ddd MMM d yyyy"))
}

Showing the Date in correct format in a Smartgwt project

I am getting a date from JSON in the following format 22 07 2014 12:04:12.
This is the code I have written:
field.setCellFormatter(new CellFormatter() {
#Override
public String format(Object arg0, ListGridRecord arg1, int arg2, int arg3) {
final DateTimeFormat fmt = DateTimeFormat.getFormat("dd MM yyyy hh:mm");
final Date date = (Date) arg0;
arg1.setAttribute(name, date);
return fmt.format(date);
});
where field is the Date field in ListGrid.
But this displays a blank field in the ListGrid. I can't figure out a way to show it in the following format:
22 Jul 2014 00:04
According to the documentation, the server must send dates with the following format:
dateField: "2007-04-22"
timeField: "11:07:13"
dateTimeField: "2007-04-22T11:07:13"
dateTimeField: "2007-04-22T11:07:13.582"
You can achieve that with this snippet:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.format(myDate);
Then, in the client side, you can format the date with SmartGWT:
myListGridField.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATETIME);