I searched a lot without finding a solution.
I would like to get the current Date time format by locale (https://en.wikipedia.org/wiki/Date_format_by_country)
For example, if my locale zone in the U.S. I would like to get "MDY" if Europe "DMY" and so on.
Is there a way to get this?
You can use the intl package and try this:
DateFormat.yMd(Localizations.localeOf(context).languageCode).format(date);
Just simply give the formate style and then fetch it..
DateFormat("dd/MMM/yyyy").format(date);
intl is not enough.
intl will only format a DateTime with a given date format String such as dd/MMM/yyyy.
In order to get users preferred date format String
you can use system_date_format
Here is an example of how to combine intl with system_date_format to achieve your goal.
final date = DateTime.now();
final dateFormatString = SystemDateTimeFormat().dateFormat;
print(dateFormatString ); // e.g. "M/d/yy"
final formattedDateString = DateFormat(dateFormatString).format(date);
print(formattedDateString ); // "2/14/23"
You could use the dateTimeSymbolMap:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/date_symbols.dart';
void main(List<String> args) {
var dateTimeSymbolMap2 = dateTimeSymbolMap()['en'] as DateSymbols;
print(dateTimeSymbolMap2.DATEFORMATS); // prints [EEEE, MMMM d, y, MMMM d, y, MMM d, y, M/d/yy]
}
Related
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));
I would like to know how a date such as "2022-07-17T01:46:12.632892+05:30" be converted to a Human Readable date in DD/MM/YYYY and hh:mm:ss format? I probably have not surfed through a lot of other questions and suggestions on the Internet but the ones I came across were not of any help. Also, what are such date formats(like the one in question) called?
It is rather straightforward using DateFormat from the package intl which comes with Flutter:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime)); // 16/07/2022 20:16:12
}
The time has here been converted to UTC to make the example the same for all readers. If not, the created DateTime would be localtime which uses the timezone on the device which the program are running.
If you want to print the time using the timezone offset of 5 hours and 30 minutes, you can do something like this:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime.add(const Duration(hours: 5, minutes: 30))));
// 17/07/2022 01:46:12
}
i would like some help on this i actually use TZDateTime and also with specific location now i would to remove the end of the result like .123436+100 from it
final dateTime = TZDateTime.now(getLocation(box.read(timezone)));
the result always i get is something like this
2022-04-20 17:42:05.032173+1000
but i want is only like this
2022-04-20 17:42:05
how can i remove that actually
I haven't used TZDateTime before but I think it's the same as DateTime. By using DateFormat from intl you can format the string as you want.
ps1: in case TZDateTime cant parse directly, you can turn it to string and pass to DateTime.parse.
ps2: this answer of mine is an idea and has not been tested correctly, sorry if it didn't help you.
import 'package:intl/intl.dart';
void main() {
var time = DateTime.parse('2022-04-20 17:42:05.032173+1000');
var fm = DateFormat('yyyy-MM-dd hh:mm:ss');
print(fm.format(time)); // -> 2022-04-20 07:42:05
}
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);
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;