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 am trying to format my date and time.
My code
String dateStart = data[i]['notification_date'];
DateTime input = DateTime.parse(dateStart);
String datee = DateFormat('hh:mm a').format(input);
its showing error Unhandled Exception: FormatException: Invalid date format
right now its look like this 22-04-2021 05:57:58 PM
You have an issue in following line:
DateTime input = DateTime.parse(dateStart);
Thing is that default parse method does not support '22-04-2021 05:57:58 PM' format, as it is not standard. You should specify its format to parse like this:
String dateStart = '22-04-2021 05:57:58 PM';
DateFormat inputFormat = DateFormat('dd-MM-yyyy hh:mm:ss a');
DateTime input = inputFormat.parse(dateStart);
String datee = DateFormat('hh:mm a').format(input);
Related
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)
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
This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 2 years ago.
I am getting date in String and I want to convert it into given Date format. Can anyone please guide me how can I do it ?
String myDate = 'June-2020'
I am getting date like this and I want to convert it into this 2020-06-01 format. 01 is default for every month.
var convertedDate = '2020-06-01'
You can create custom date format using intl package
String dateString1 = 'June-2020';
String dateString2 = 'May-2021';
DateFormat dateFormate = DateFormat('MMMM-yyyy');
DateTime date1 = dateFormate.parse(dateString1);
DateTime date2 = dateFormate.parse(dateString2);
print(date1); //2020-06-01 00:00:00.000
print(date2); //2020-05-01 00:00:00.000
This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 2 years ago.
I am working in Swift language. I don't know the date format for this 2020-07-23T00:17:06.000Z.
I want to convert Date to String by following that format & vice versa.
How can I do? Thanks in advance...
it is iso8601
let string = "2020-07-23T00:17:06.000Z"
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = dateFormatter.date(from: string)
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