How to convert correctly only time in dart - flutter

i've a string like this
String time = '13:00:00Z'; (Zulu time)
I want to convert it into a string in the correct local time.
For example if i execute the program, i want 15:00 as a result because 13:00 in my local time it's 15:00.
I get the error "FormatExeption: Invalid date format", whatever I put in the DateFormat

You can prepend current date to the time string and then use toLocal() to convert it to your local time:
DateTime date = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd').format(date);
String time = "13:00:00Z";
DateTime dt = DateTime.parse(formattedDate + "T" + time).toLocal();
String formattedTime = DateFormat('HH:mm').format(dt);
print(formattedTime);
(DateFormat is from the intl package)

Related

TemporalDate from DateTime - Flutter

I am using AWS amplify for my project and trying to save the selected date from datepicker to the model.
DatePicker is giving the date in DateTime object, when i try to convert it to TemporalDate its giving me a day before date. i.e if i select todays date and assigning it to the TemporalDate then i am getting yesterday's date.
Here is my code :
var d1 = TemporalDate(pickedDate);
var d4 = TemporalDate(pickedDate.toUtc());
var d2 = TemporalDate(pickedDate.toLocal());
var d3 = TemporalDate(pickedDate).getDateTime();
pickedDate is holding today's date but the d1,d2,d3,d4 all is having yesterday's date.
Expected result is to get the today's date in the TemporalDate variable.
You need to parse the date. It is showing a day before because it is on the universal date. So you just need to convert the universal date to local date.
var dateTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(selectedDate.toString(), true);
var dateLocal = dateTime.toLocal();

How to convert unix timestamp to iso 8601 in Flutter

I am getting date from a server as a unix timestamp, how can I convert it to ISO 8601 date format in flutter?
the date I receive:
1611694800000
How I want to convert it to be
2021-01-26T22:00:00.000+00:00
What I have done so far with no luck
String s = '1611694800000';
debugPrint("Recevied date is: $s");
String dateS = DateTime.parse(s).toIso8601String();
debugPrint("Converted date : $dateS");
String dateStr = (dateS.split(".")[0].split("T")[0] + " 00:00:00").substring(1);
debugPrint("Activation date: $dateStr");
I end up getting:
Unhandled Exception: FormatException: Invalid date format.
Use DateTime.fromMillisecondsSinceEpoch:
var timestampMilliseconds = 1611694800000;
var datetime =
DateTime.fromMillisecondsSinceEpoch(timestampMilliseconds, isUtc: true);
print(datetime.toIso8601String()); // Prints: 2021-01-26T21:00:00.000Z
(Note that the printed time is one hour off of your stated expectation, but I'm assuming that's a mistake in your expectation.)
The reason why you are getting invalid date format is because you have to provide date in string like '2021-04-19' and not milliseconds;
This package makes it easy to format dates time_formatter

Convert time AM and PM TO 24 hour format

String timer = "01:30 PM"
String = "12/10/2020"
DateTime fdate = DateFormat("yyyy-MM-dd").parse(dates);
DateTime ftime = DateFormat("hh:mm:ss").parse(timer);
I am getting the error while converting the string time into the Original time format. How to convert that time and date into the different format.
How to get the combination of date and time like 2020-10-12 13:30:00
Change ftime to :
DateTime ftime = DateFormat("HH:mm:ss").parse(timer);
Consider reading DateFormat Documentation for more information

how to convert time string (YYYY-MM-DD) into xx Day xx Hr xx min remaining in Dart?

I want to convert time string (YYYY-MM-DD) into "xx Day xx Hr xx min remaining" .
How can I do that in dart?
You can Use the Datetime class to find the difference between the two years without using intl to format the date.
You Can also use Intl Package for same
DateTime dob = DateTime.parse('1967-10-12');
Duration dur = DateTime.now().difference(dob);
String differenceInYears = (dur.inDays/365).floor().toString();
return new Text(differenceInYears + ' years');

Joda: how to generate an ISO datetime string with "Z" at the and

I generate an ISO datetime string (without time zone) like this:
val dateTime: LocalDateTime = LocalDateTime.now
val dateTimeStr: String = ISODateTimeFormat.dateTime.withZone(DateTimeZone.UTC).print(dateTime)
The code above produces the following string:
2014-04-10T06:13:19.283
Now I need to convert this string back to a LocalDateTime...
val dateTime = LocalDateTime.parse(dateTimeStr, ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC))
... and compare it with the current time:
val isBefore = dateTime.isBefore(LocalDateTime.now)
The code above doesn't work and produces the following error:
Invalid format: \"2014-04-27T17:51:06.780\" is too short
To fix the problem, I need to append a Z to dateTimeStr:
val dateTime = LocalDateTime.parse(s"${dateTimeStr}Z", ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC))
Is there a way to generate the ISO datetime string with the Z at the end?
A LocalDateTime has NO timezone. So you cannot associate a timezone-aware format (Z stands for UTC timezone) with this zoneless data type.
If you insist on having Z-formats then you have to work with a global type like DateTime. So you have two different steps. One step is object conversion between local and global type:
LocalDateTime ldt = ...;
DateTime dt = ldt.toDateTime(DateTimeZone.UTC); // or another timezone
// reverse
DateTime dt = ...;
LocalDateTime ldt = dt.toLocalDateTime();
Second step is conversion between formatted string form and global timestamp:
LocalDateTime ldt = ...;
DateTime dt = ldt.toDateTime(DateTimeZone.UTC); // or another timezone
String iso = ISODateTimeFormat.dateTime().print(dt);
// reverse
String iso = ...; // with trailing Z or another timezone information
DateTime dt = IsoDateTimeFormat.parseDateTime(iso);
LocalDateTime ldt = dt.toLocalDateTime();
Finally you can combine these two steps for conversion between ISO-format with timezone information and a LocalDateTime as shown in the second step.
If you only need formatted strings without any timezone or offset information then you can stick the global type DateTime completely and just use localDateOptionalTimeParser() as #Sebastian has correctly mentioned.
Try to use
val dateTime: DateTime = DateTime.now
instead of
val dateTime: LocalDateTime = LocalDateTime.now
Or if you want to stick to LocalDateTime change the parsing:
val dateTime = LocalDateTime.parse(dateTimeStr) // this uses the default localDateOptionalTimeParser