How to De format the DateTime in flutter? - date

I have formatted the date in flutter with specific format like "01-Jan-2000", Now How can I convert it back to the DateTime in flutter?

Use DateFormat like this:
DateFormat dateFormat = DateFormat("dd-MMM-yyyy");
DateTime dateTime = dateFormat.parse("01-Jan-2000");

Try this package, Jiffy. You can solve this in one line of code
DateTime dateTime = Jiffy("01-Jan-2000", "dd-MMM-yyyy").dateTime;

Related

DateTime parse issue using intl package dart

I have a date which is a string and looks like this:
String day = "30-11-2022 12:27";
I am trying to convert the above string to DateTime object and convert the 24hr time to 12hr. I am using the following code:
DateFormat("dd-MM-yyyy hh:mm a").parse(day);
It was working before but today the parsing is causing format exception error. The error message is shown below:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Trying to read from 30-11-2022 12:27 at position 17
Why am I getting error while parsing now? How to fix it?
There must be a simpler solution, but it works this way:
final str = "30-11-2022 12:27";
final date = DateFormat("dd-MM-yyyy hh:mm").parse(str);
final formated = DateFormat("dd-MM-yyyy h:mm a").format(date);
The format for a 24-hr time is 'HH:mm'. The key is to call add_jm(). Just do this:
final day = '30-11-2022 12:27';
final dateTime = DateFormat('dd-MM-yyyy HH:mm').parse(day);
final formatted = DateFormat('dd-MM-yyy').add_jm().format(dateTime);
print(formatted);
The output is:
30-11-2022 12:27 PM
can try this. hope your problem solved
DateTime dateTime = DateFormat('yyyy/MM/dd h:m').parse(date);
final DateFormat formatter = DateFormat('yyyy-MM-dd h:m a');
final String formatted = formatter.format(dateTime);
It really doesn't matter for datetime that the input date is in am format or not, because you can parse it to what ever format you want. For both option when you parse your string, it will save it in regular form. So just try this:
String day = "30-11-2022 12:27";
DateTime date = normalFormat.parse(day);
and when ever you want show it as AM, just format date;
Why am I getting error while parsing now? How to fix it?
You specified a DateFormat format string that uses a, which indicates that you want to parse an AM/PM marker, but the string you try to parse is "30-11-2022 12:27", which lacks it.
If you're trying to convert a 24-hour time to a 12-hour time, those are fundamentally two different formats, so you will two different DateFormat objects. Note that the format pattern for hours is different for 12-hour times (h) than for 24-hour ones (H):
String day = "30-11-2022 12:27";
var datetime = DateFormat("dd-MM-yyyy HH:mm").parse(day);
// Prints: 30-11-2022 12:27 PM
print(DateFormat('dd-MM-yyyy hh:mm a').format(datetime));

how to show timestamp from firestore in dd/mm/yy format in flutter

how to show datetime(timestamp format) form firebase firestore in (dd/mm/yy, hh:mm:ss) in flutter.
please see the images attachedfirebase firestore data
and my code is my code in vscode
You can simply call toDate() function to the dateTime or your firebase timestamp.
You can also convert them into desired format by using DateFormat class
Here is a small function which will return time like 12:37 AM :
import 'package:intl/intl.dart'; //add this import statement for using DateTime class
String getTime(var time) {
final DateFormat formatter = DateFormat('dd/MM/yyyy, hh:mm:ss aa'); //your date format here
var date = time.toDate();
return formatter.format(date);
}
This function will convert your timestamp object to provided format
eg.: July 23, 2021 at 9:22:29 PM UTC+5:30 -> 23/07/2021, 9:22:29 PM
You can refer this document for detailed date formatting.
You can first parse the date to get a DateTime object by using DateTime.parse(string_from_firebase).
Then use the DateFormat class from the intl package.
final DateTime dateToBeFormatted = DateTime.parse(string);
final df = DateFormat('dd/MM/yyyy');
final formatted = df.format(dateToBeFormatted);

Flutter how to convert timestamp date time

I am getting date and time from store. In data base its look like this
Need to know how can I show this as DD/MM/YY
I am trying to do like this
String timeString = snapshot.data[index]['lastupdate'].toString();
DateTime date = DateTime.parse(timeString);
print(DateFormat('yyyy-MM-dd').format(date));
Its showing error of Invalid date format
Try like this your lastupdate date is not convertime inDate thats why its showing error
DateTime date = DateTime.parse(snapshot.data[index]['lastupdate'].toDate().toString());
print(DateFormat('dd-MMM-yyy').format(date));
Use function to get date from Timestamp like this:
readDate(Timestamp dateTime) {
DateTime date = DateTime.parse(dateTime.toDate().toString());
// add DateFormat What you want. Look at the below comment example
//String formatedDate = DateFormat('dd-MMM-yyy').format(date);
String formatedDate = DateFormat.yMMMMd().format(date);
return formatedDate;
}
Use the function when you want it.
For example:
Text(readDOB(streamSnapshot.data!["dob"]))
For this you should install intl package. read

How to get a date in the format "2020-01-10T08: 47: 36.264Z"?

I am trying to get a date in the format "2020-01-10T08: 47: 36.264Z", but I have not succeeded, I try to use the toIso8601String (), toUtc (), toLocal () method, but I do not return the format required.
I am new to flutter.
Thanks.
To format a date in Flutter, you can follow this step:
Import the intl package.
Create a method to format your date like this.
String formatDate(DateTime date) { DateFormat formatter = DateFormat ('yyyy-MM-dd');
return formatter.format(date); }
So you will go to where you want to format your date and call the formatDate method and pass your date.
Would this help?
DateTime now = DateTime.now();
String formattedDate = DateFormat('dd-MM-yyyy HH:mm:ss').format(now);

How to parse a Datetime with format "MM/dd/YY" in flutter

So far I try new DateFormat("MM/dd/yy").parse('04/03/20') but it produce 04/03/0020 date.
You have to formate this date using formate method of datetime class. However, you need to convert string to datetime using parse method because datetime formate take datatime as an argument.
Following line help you to achieve desire output:
print(DateFormat("MM/dd/yy")
.format(DateFormat("MM/dd/yy").parse('04/03/20')));
UPDATE:
This following code will give result as you expected but it is hard to find out it is 1920 or 2020?
DateTime _dateTime = DateFormat("mm/dd/yy").parse('04/03/20');
_dateTime = DateTime(2000 + _dateTime.year, _dateTime.month, _dateTime.day);
print(_dateTime);