How to format flutter DateTime
as 10th Oct 2020
tried
String date = DateFormat("dd, mm, y").format (date.utc());//10 October 2020
you can use the Jiffy package from here ==> https://pub.dev/packages/jiffy.
this is an example of formatting DateTime:
import 'package:jiffy/jiffy.dart';
void main() {
DateTime currentTime = DateTime.now();
String result = Jiffy(currentTime).format('MMM do yy');
print(result);
}
Try out this package, Jiffy
Just simply add the do date pattern. See below
Jiffy([2020, 10, 10]).format("do MMM yyyy"); //10th Oct 2020
You can also add your DateTime object
Jiffy(DateTime(2020, 10, 10)).format("do MMM yyyy"); //10th Oct 2020
Related
I'm getting a expiry time when i call the log in api in for format of "Thu, 30 Dec 2021 10:24:34 GMT" i want to get the current time and compare it.
By using DateFormat class in intl package,
you can parse from string date to DateTime.
And by using 'isBefore' or 'isAfter' method, you can compare two DateTime.
import 'package:intl/intl.dart';
void main() {
String strDate = "Thu, 30 Dec 2021 10:24:34 GMT";
DateFormat format = DateFormat("EEE, dd MMM yyyy hh:mm:ss");
DateTime parsedDate = format.parse(strDate);
print('parsedDate: $parsedDate');
DateTime now = DateTime.now();
print('now: $now');
print('parsedDate is before now: ${parsedDate.isBefore(now)}');
print('parsedDate is after now: ${parsedDate.isAfter(now)}');
}
If I understand your question correctly, this can help.
DateTime now = DateTime.now();
DateTime then = DateTime.parse(expirlyDateFromApi.toDate().toString());
var difference = now.difference(expirlyDate);
According to the docs for the dart intl package, DateFormat.jm() is for formatting time as AM and PM, and DateFormat.Hm() is for using the 24 hour format:
DateFormat.jm() -> 5:08 PM
DateFormat.Hm() -> 17:08 // force 24 hour time
However, after configuring Intl to use the user's locale by calling await findSystemLocale(), I don't see any difference between the two:
import 'package:intl/intl.dart';
import 'package:intl/intl_standalone.dart';
...
await findSystemLocale(); // <-- running this causes the problem
final now = DateTime.now();
print("${Intl.defaultLocale} ${DateFormat.jm().format(now)} ${DateFormat.Hm().format(now)}");
Gives me:
en_GB 16:45 16:45
If I omit await findSystemLocale(); I get the expected result, but in the default locale:
en_US 5:51 PM 17:51
Is this a bug or am I misunderstanding the docs?
I'm using intl v0.16.1.
Hm... in my case I have a below result.
flutter 1.22.5
intl 0.16.1
en_US 4:45 PM 16:45
If that, how about you use custom format like below?
DateFormat('h:mm a').format(now)
Try this one
final now = DateTime.now();
var time = DateFormat.jm().format(now);
print(time);
final now = DateTime.now();
var time = DateFormat.Hm().format(now);
print(time);
I am trying to millisecondsSinceEpoch 1619451874928 to dd-MM-YYYY format in flutter. Is there any method that does that?
How to convert eg. 1619451874928 to 27 March 2021
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.fromMillisecondsSinceEpoch(1619451874928);
final dateTimeString = DateFormat("dd-MM-yyyy").format(dateTime);
print(dateTimeString);
final dateTimeStringLong = DateFormat("dd MMMM yyyy").format(dateTime);
print(dateTimeStringLong);
}
dateTimeString will be in format you requested, i.e. "dd-MM-yyyy". Hovever in order to get date in format of "27 March 2021", you should use following pattern
"dd MMMM yyyy".
Please note that you should also include and import package 'package:intl/intl.dart'
You can use this:
DateFormat("dd MM yyyy").format((DateTime.fromMillisecondsSinceEpoch(1619451874928)));
This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 1 year ago.
I want to convert string to DateTime in dart. I tried,
String timeVal = "Thu, 25 Mar 2021 19:29:28 GMT";
DateTime.parse(timeVal);
but getting an exception,
FormatException (FormatException: Invalid date format Thu, 25 Mar 2021
19:29:28 GMT)
How could I convert such a string value to DateTime in dart
You can use the intl package:
import 'package:intl/intl.dart';
void main() {
final string = 'Thu, 25 Mar 2021 19:29:28 GMT';
final formatter = DateFormat('EEE, d MMM yyyy HH:mm:ss');
final dateTime = formatter.parse(string);
print(dateTime); // prints : 2021-03-25 19:29:28.000
}
For more info, see this answer and the DartFormat class docs.
Please note, the parsed DateTime in the example does not have the correct time zone (ie dateTime.timeZoneName will return your local time zone).
From the user I receive a DateTime variable. I want to get the name of the month that was entered. Is there a way to do so? (Apart from having a bunch of if statements)
Use the intl package.
DateFormat("MMMM").format(dateTime);
Another way to get name of month:
List months = ['jan','feb','mar','april','may','jun','july','aug','sep','oct','nov','dec'];
var someDateTime = new DateTime.now();
var mon = someDateTime.month;
print(months[mon+1]);
don't forget to import package:
import 'package:intl/intl.dart';
Use it:
import 'package:intl/intl.dart';
//--------------------
final DateTime today = DateTime.now();
final DateFormat format1 = DateFormat('MMM');
final DateFormat format2 = DateFormat('MMMM');
print(format1.format(today)); // abbreviated, exp. : Jan, Feb, Mar
print(format2.format(today)); // not abbreviated, exp. : January, February,