Is there a way to convert millisecondsSinceEpoch to date,month year format in DateFormat? - flutter

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

Related

How to convert to readable dates in Dart

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
}

Flutter: Formatting DateTime in flutter

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

I get a expiry date and time from the api how compare it with the current time?

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

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

How do I get the name of a month

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,