I have an instance of DateTime and I would like to format that to a String. How do I do that? I want to turn the date into a string, something like "May 14, 2020". I have not seen any solution yet.
You can use the intl package to formate DateTime.
To get "May 14, 2020" you can try the code below.
DateFormat('MMMM dd, yyyy').format(dateTime)
Related
I have a problem with Dart intl package while using DateFormat:
formatDateTime(DateFormat("yyyy-MM-dd hh:mm").parse(fortune.readFinishedAt ?? "2025-12-27 12:27:45"))
When I use this code, I want to take 12:27 for the hour part but DateFormat gives 00:27 to me.
hour property is 0.
How can I solve this issue?
If you need to the 24 hour format use this DateFormat("yyyy-MM-dd HH:mm").
use formate like I mention below:-
formatDateTime(DateFormat("yyyy-MM-dd hh:mm a").parse(fortune.readFinishedAt ?? "2025-12-27 12:27:45")),
use hh:mm a it will gave you time in am/pm formate
It seems like you want to use 12 hr time period
use formate like I mention below:-
if yopu want to use 12 hr time
use (hh:mm) a insted of (hh:mm)it will gave you time in am/pm formate
formatDateTime(DateFormat("yyyy-MM-dd hh:mm a").parse(fortune.readFinishedAt ?? "2025-12-27 12:27:45"))
Use this,
formatDateTime(DateFormat("yyyy-MM-dd h:mm:ss").parse(fortune.readFinishedAt ?? "2025-12-27 12:27:45")),
For more info : https://api.flutter.dev/flutter/intl/DateFormat-class.html
I am retrieving a date from api in this format, e.g "2020-03-02". But i need to a render it as this format e.g, "Tuesday, March,2020". I have been unable to find consice solution to this with flutter. Any help?
Use intl plugin
print(DateFormat('EEEE, MMM, yyyy').parse(dateString)); // prints Tuesday, mar, 2020
//OR
print(DateFormat('yyyy, MMM, EEEE').parse(dateString)); // prints 2020, mar, Tuesday
I'm trying to convert a string to a date and I'm able to do it when my string is in this format: 14/09/2016 or 09-14-2016, etc. using the new Date().parse('dd/MM/yyyy', myDateString) method. But now I have a string that looks like this: 14 September 2016. So what I want to know is what format do I give the parse method to make this work. new Date().parse('/*what goes here?*/', myDateString). Also will this method even work?
Any help would be appreciated. Thanks
Date.parse is a wrapper for SimpleDateFormat.parse, so that's where you'll want to look to find an appropriate format string. In the case of "14 September 2016", your format string would look like this: "dd MMM yyyy".
The key part for parsing this string is "MMM". From the docs:
Month: If the number of pattern letters is 3 or more, the month is
interpreted as text; otherwise, it is interpreted as a number.
I have a ISO8601 string (e.g. date="2015-07-10T04:31:25") I need to convert this to the format:
July 7, 2015, 4:31:25 PM (EDT)
Even though I can write a template and use substring() to transform the string in the date time format. However I am not sure how to achieve the AM/PM and time zone information?
Working code templates would be highly appreciated. Thanks!
I am not sure how to achieve the AM/PM ...
It can be calculated from the hour component as:
substring('AMPM', 1 + 2*(number($hour) > 11), 2)
Of course, in the given input, where $hour would be "04", the correct result is "AM", not "PM".
... and time zone information?
Your input does not contain any time zone information, so unless you want to hard-code EDT as a string, there is no way to get it.
i want to convert my NSDate to a certain format which is:
"Wednesday, December, 2, 2009"
does anybody know what is the corresponding format string to that date example?
appreciate your help!
Look here for date formatting syntax, looks like what you need would be #"%A, %B,%d,%Y"