Get correct date from excell on flutter - flutter

Hello guys I'm having a little hard time figuring this out.
So I have a small excell that I'm putting some information there(adding and requesting)
The problem is when I'm trying to get the date as string, and add it as DateTime.
Always the same error "Invalid Date Format"-
I have my dates on excell, as Simple Text saved as "20-06-2022", and displaying that on flutter with "user[index].date", all ok. The problem is that I want to compare the dates with a random day.
I've tried
DateTime.parse(users[index].date); // not working
Text(users[index].date); // not working ( shows random numbers as 44734)

The DateTime.parse method only accepts specific formats that are listed in the documentation.
Since yours is not one of those, you need to create a DateFormat instance of your own and use that one to parse
void main() {
final text = '20-06-2022';
final format = DateFormat('dd.MM.yyyy');
final date = format.parse(text);
print(date);
}

Related

DateFormat dateFormat = DateFormat("yyyy-MM-dd"); To Month-Day-Year

DateFormat dateFormat = DateFormat("yyyy-MM-dd"); This line of code will not let me change the DateFormat to ("MM-dd-yyyy") without showing throwing an error in the app, or not displaying correctly.
I just need it to show MONTH-DAY-YEAR, NOT YEAR-MONTH-DAY
Flutter refuses to let me change it. Nobody seems to have an answer for how to change it. I've been looking for two weeks on how to change it with no luck.
Also asked here: Flutter DatePicker change format to MM-DD-YYYY, but none of the answers worked either.
Here's the error being thrown in the app:
EDIT: More Code Context.
The original line was this:
DateFormat dateFormat = DateFormat("yyyy-MM-dd");
DateTime dateTime = dateFormat.parse(ticketModel.date);
dateTime was then getting shown via Text in a container this way:
child: Text(dateTime.toString().split(' ').first.toString(),
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
letterSpacing: 2.5,
color: Colors.white,
)),
This is NOT my code, and I am not a Flutter dev.
Something is happening in these lines that won't allow MM-dd-yyyy to work properly.
The error is a photo, because it is getting displayed in the app container, not in any sort of terminal. I didn't want any mistakes if I tried copying it to text. I know its frowned upon, this isn't my first rodeo getting scolded on Stackoverflow, but I was desperate here.
My issue was solved for now by following that person's answer below and changing my code to this:
DateFormat dateFormat = DateFormat("yyyy-MM-dd");
DateTime fudgeThis = dateFormat.parse(ticketModel.date);
String dateTime = fudgeThis.showDateInOwnFormat();
Hopefully this edits help someone. Seriously.
let's say I have the DateTime variable
DateTime ourDateExample = DateTime.now();
you can create your own extension on DateTime to achieve what you want
extension ShowDataInOwnFormat on DateTime {
String showDateInOwnFormat() {
return '$month-$day-$year';
}}
put this outside of your classes or on a separate file, then call it whenever you want like this :
ourDateExample.showDateInOwnFormat();
it will return the date as the Month-day-year
Thank you for finally showing your code. However, the code you showed is not the code that generated the error. The point of asking for the exact code that you tried is not to be rude or sassy; it's to allow other people to reproduce your problem and to determine its exact cause, such as whether your problem was caused by a typo or by some other mistake. As evident from the error, what you actually tried was MMMM instead of MM, which would be a typo.
Based on your code and on the error message, I also suspect that you accidentally reused the same DateFormat object for parsing and printing. That is, you probably tried something like:
DateFormat dateFormat = DateFormat("MMMM-dd-yyyy");
DateTime fudgeThis = dateFormat.parse(ticketModel.date);
String dateTime = dateFormat.format(fudgeThis); // WRONG
That would not work if you're trying to convert between formats. You instead should have used separate DateFormat objects:
DateTime fudgeThis = DateFormat("yyyy-MM-dd").parse(ticketModel.date);
String dateTime = DateFormat("MM-dd-yyyy").format(fudgeThis);
Unlike naively creating your own string from the month, day, and year components directly, the above will format the date with the specified minimum number of digits.
Additionally, your error message indicates that you were attempting to parse an ISO-8601 date string, so you probably don't need to use DateFormat for parsing anyway:
DateTime fudgeThis = DateTime.parse(ticketModel.date);
String dateTime = DateFormat("MM-dd-yyyy").format(fudgeThis);

Flutter: Display local datetimes correctly

let's say I have a DateTime object and I want to display it in the correct local format.
If I do the following on a German device I get this:
dateTime.toLocal().toString()
// Prints
2022-05-28 23:29:19.518
However, I would expect or desire more something like this for a German device: 28.5.2022 23:29:19
I know that I can format the DateTime but that would just be hardcoding it for a certain locale.
Weirdly enough all the solutions that I found for this on StackOverflow are either hardcoding the format or only apply to Dart, not Flutter.
What is the correct way to display a local datetime in Flutter?
You can use this package intl and localise dates like
var format = DateFormat.yMd('ar');
var dateString = format.format(DateTime.now());
Using the intl package which was mentioned here already, this has been working well for me so far:
DateFormat dateTimeFormat = DateFormat.jm(Localizations.localeOf(context).toString());
DateTime dt = DateTime.fromMicrosecondsSinceEpoch(entity.syncDateTime);
dateTimeFormat.format(dt);
To get outputs which are not yet supported I, for example, concat a ymd formatted DateTime string with a jm formatted DateTime string.

Can I use dt_txt instead of dt for date using flutter?

I've been having problems rendering the dates even after formatting the dates.. I'm using openweather api for 3 hours forecast and trying to render the date as dt_txt format. Can I just use dt_txt for date instead of dt converting to simple date?
Without knowing how you retrieve the other data from the list it is hard to point you in the right direction. But I'll give it a try.
According to the docs cnt means that the list has 40 objects in your case.
You're only using the first one [0] over here:
date: DateTime.fromMillisecondsSinceEpoch(
json['list'][0]['dt'] * 1000,
isUtc: false)
If you want to use dt_text from each object you need to do something this using an index.
json['list'][index]['dt_txt']
A simple example using a for loop could also be:
List<String> dateList = [];
for(var data in json['list']){
dateList.add(data['dt_txt']);
}

DateTime in Flutter

I was importing internationalization for my app but I have a problem where I parse my String but it only returns the date and not the time. When I change the phone language, my format for the Date changes but my Time isn't showing.
This is when it is EN_US
This is when it is EU(German)
That works well but my time format is missing...
This is the default String from the API:
Here is the code:
/// I set a variable that calls the locale of the phone language and changes according to the phone preference
final f = DateFormat.yMd(Localizations.localeOf(context).languageCode);
/// where I dispay time and date but only date is shown in the pictures above
Text('Time and date: ${f.format(DateTime.parse(apicall.dateCreated))}');
My questions are: How do I show the time and how can I swap between showing the Time first then the Date, and opposite, if I want to show the Date first then the Time? (example 18-May-21 11:10h and if I want to do this 11:10h 18-May-21)
Thanks in advance for the help!
I suppose you should use DateFormat.jm().add_yMd(): this will show time first and then date. To swap it just use DateFormat.yMd().add_jm()
More info here

Export Calendar Date to spreadsheetout - Time Stripped off - Google Script

I am using Google Script to export some calendar events to a spreadsheet; the relevant portion of my script is below:
var details=[[mycal,events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), events[i].getStartTime(), myformula_placeholder, ('')]];
var range=sheet.getRange(row,1,1,7);
range.setValues(details);
This code works but the "time" that is put into the spreadsheet is a real number of the form nnnnn.nn. On the spreadsheet itself the date looks great using the integer to the left of the decimal (eg 10/15/2017) but the decimals are part of the value and therefore are part of the spreadsheet value.
My script drops the data into a sheet in my workbook, and another sheet reads the rows of data with the above date types, looking for specific date info from the other sheet using the match function (for today()). That would work fine if I could get rid of the decimals.
How can I use what I have above (if I stray far from what I have found works I will be redoing hours of work) but adding just what is needed to only put into the output spreadsheet the whole number portion so I have a pure date that will be found nicely by my match function using today()?
I have been digging, but errors abound in trying to put it all together. "Parse" looked like a good hope, but it failed as the validation did not like parse used within getStartTime. Maybe I used it in the wrong manner.
Help would be appreciated greatly.
According to the CalendarApp documentation, getStartTime() generates a Date object. You should be able to extract the date and time separately from the date object:
var eventStart = events[i].getStartTime(); // Returns date object
var startDate = eventStart.toDateString(); // Returns date portion as a string
var startTime = eventStart.toTimeString(); // Returns time portion as a string
You could then write one or both of these to your spreadsheet. See the w3schools Javascript Date Reference for more information:
http://www.w3schools.com/jsref/jsref_obj_date.asp
If you If you want to specify the string format, you can try formatDate in the Utilities service:
https://developers.google.com/apps-script/reference/utilities/utilities#formatdatedate-timezone-format
You could just use the Math.floor() function
http://www.w3schools.com/jsref/jsref_floor.asp
which will round the real number to an integer. Your line would then read:
var details=[[mycal,events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), Math.floor(events[i].getStartTime()), myformula_placeholder, ('')]];