GMT time String to DateTime in dart [duplicate] - flutter

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

Related

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

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

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

Convert epoch time into timestamp flutter

I have a date-time in epoch format: 1633247247
I want to convert it into timestamps like this: Sunday, 3 October 2021 or just October 3 2021
I am writing this code
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247);
But it is returning 1970-01-19 18:25:11.247
Edit I ran this code
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
Got the output in datetime. I am now trying to convert into string
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(date);
It gives this error The instance member 'date' can't be accessed in an initializer.
It represent to milliseconds, you need to multiple it with 1000 like below :
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
It seems like that the time you've is 'seconds since epoch' so just multiplying by 1000 should give you the correct time.
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
Download and Import
intl Pacakge
import 'package:intl/intl.dart';
Initialize a variable
String datetime;
Get Current date and conversion
Future<void> Datetimes() async{
setState(() {
DateTime now = DateTime.now();
String formattedDate = DateFormat('EEEE d MMM hh:mm:ss').format(now);
datetime=formattedDate;
});
}
Output
Saturday 3 oct 2:07:29
Refer this for DateandTime class for more formatting parameters
DateFormat-class
You need to use DateTime Formats. You can find a solution here:
https://stackoverflow.com/a/51579740/11604909

Unable to parse "CET" from formatted DateString

Why the DateFormat Parser cant parse CET?
final String dateString = "Sat Jul 05 00:00:00 CET 1975";
final DateTime dateTime =
DateFormat("EEE MMM dd HH:mm:ss vvv yyyy").parse(dateString);
Results in
FormatException: Trying to read from Sat Jul 05 00:00:00 CET 1975 at position 21
Position 21 would be "C" if I count right.
Anybody had this problem before?
intl package didn't implement parsing time zones yet, as evident in date_format_field class :
case 'v':
return formatTimeZoneId(date);
case 'z':
return formatTimeZone(date);
case 'Z':
return formatTimeZoneRFC(date);
And when you look at these functions implementation :
String formatTimeZoneId(DateTime date) {
// TODO(alanknight): implement time zone support
throw new UnimplementedError();
}
String formatTimeZone(DateTime date) {
throw new UnimplementedError();
}
String formatTimeZoneRFC(DateTime date) {
throw new UnimplementedError();
}
This issue has been open since 2015, and this is the response of one of the people responsible for it :
We don't expect to implement those until Dart DateTime's have time zone information, and that's not planned. I wouldn't expect it unless/until JavaScript DateTime's have them. Including localized TimeZone names is a lot of data. We should probably just remove those APIs until such time as we actually provide them.