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
Related
I'm trying to create a TimeSeriesChart with a specific DateTimeTickFormatterSpec that formats dates in the current year as MMM and everything else as yMMM. In other words, the month should always be shown and the year should only be shown if it's not the current year.
01-01-2020 => Jan 2020
12-01-2021 => Dec 2021
01-01-2022 => Jan
I'm currently looking at charts.BasicNumericTickFormatterSpec.fromNumberFormat() which takes in a DateFormat for formatting the chart's ticks. Addressing the DateFormat for this case appears to be the simplest approach to me.
I wanted to ask if anyone knew if a conditional DateFormat is achievable or if anyone had any insight on using DateTimeFormatterFunction to achieve the above.
Thanks.
NVM, figured it out.
You'll want to use charts.AutoDateTimeTickFormatterSpec and specify a function along with your 2 DateFormats. In my case, currentYear contains MMM and previousYear contains yMMM.
tickFormatterSpec: charts.BasicDateTimeTickFormatterSpec((date) =>
date.year == DateTime.now().year
? currentYear.dateTickFormatter!.format(date)
: previousYear.dateTickFormatter!.format(date))
when I execute this line of code:
DateTime dt = DateFormat("EEEE dd.MM.yyyy HH:MM").parse("Monday 01.12.2019 17:00");
print("dt.tostring: " + dt.toString()); //2018-12-01 17:00:00.000
The date is getting parsed from 2019 to 2018.
Tried 2020 and that is getting parsed to 2019.
Looks like a bug to me. Shall I make an Issue?
Wanted to ask here to make sure.
intl: ^0.16.1
DateTime dt = DateFormat("EEEE dd.MM.yyyy HH:mm").parse("Monday 01.12.2019 17:00");
you need to use lower-case 'mm' for minutes
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)
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 need to localize the date. i tried with following code but its giving wrong thing.
For Example if my locale is "US" if i take date like(06 August 2011, 10:55 A.M.) from date picker and store in database then after i changed to locale to "Dutch" its working fine, it converts into dutch like (06 augustus 2011 10:55)
If i take date from date picker like (06 augustus 2011 10:55) for locale "Dutch" then I am changing to locale to "US" its display like(06 July 2011 10:55 A.M.),but it should be like(06 August 2011 10:55 A.M.).
I use below code for get date local date from string:
NSDate *past = [NSDate dateWithNaturalLanguageString:#"06 augustus 2011 10:55" locale: [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
please help out me
You're going about this the wrong way. You should not store localized strings in your database, just store the actual -date returned from UIDatePicker.
Use NSDate all the time, and only when you display to the user do you use a NSDateFormatter to show the date in the user's appropriate locale.
Hope this helps!