Flutter custom date format - flutter

I am facing issue while formatting the date to custom format.
I need to convert date yyyy-MM-dd HH:mm:ss ===> EEEE, MMM dd, yyyy
For example I am getting date from server 27-10-2022 11:02:50, and I need to convert it to Thursday, October 27, 2022

Getting Date format is "dd-MM-yyyy HH:mm:ss" and the desire format will be "EEEE, MMMM dd, yyyy"
final data = "27-10-2022 11:02:50";
final format = DateFormat("dd-MM-yyyy HH:mm:ss");
final DateTime result = format.parse(data);
print(result); //2022-10-27 11:02:50.000
final newFormatter = DateFormat("EEEE, MMMM dd, yyyy");
final newFormatString = newFormatter.format(result);
print(newFormatString); // Thursday, October 27, 2022
I am using intl package

Just checked in flutter docs. Your date format is not good. For converting string to date.
The following date format is required,
"2012-02-27 13:27:00"
"2012-02-27 13:27:00.123456789z"
"2012-02-27 13:27:00,123456789z"
"20120227 13:27:00"
"20120227T132700"
"20120227"
"+20120227"
"2012-02-27T14Z"
"2012-02-27T14+00:00"
"-123450101 00:00:00 Z": in the year -12345.
"2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
And you can convert that into the format like below
var date1 = DateFormat('dd-MM-yyyy hh:mm:ss').parse("27-10-2022 11:02:50");
var date2 = DateFormat('yyyy-MM-dd hh:mm:ss').format(date1);
print( DateFormat('EEEE, MMMM dd, yyyy').format(date2));

please try this, hope you will get the idea,
print(DateFormat('dd-MM-yyyy HH:mm:ss').parse('27-10-2022 11:02:50'));

Related

Revert datetime format, from short to long

Tried searching around couldn't really find anything. Was hoping to find a way to revert the datetime format.
So I start off with: 4-11-22 and I want to change to Friday, 12 November 2022.
Using the intl package
import intl package
import 'package:intl/intl.dart';
Then, you can do as follows:
String myDate = '4-11-22'; // input date
String pattern = 'dd-MM-yy'; // define parse pattern for the input date
DateTime date = DateFormat(pattern).parse(myDate); // parse the input date
String newPattern = 'EEEE, dd MMMM yyyy'; // define new pattern
String formattedDate = DateFormat(newPattern).format(date); // reformat
print(formattedDate); // result: Friday, 04 November 2022
Try on DartPad
For more formatting possibilities, go to the docs.
final oldDateDateTime = DateFormat('dd-MM-yy').parse('4-11-22');
final newDateString = DateFormat('EEEE, d MMMM y', 'en_US').format(oldDateDateTime);
print(oldDateDateTime.toString());
print(newDateString);
Output:
2022-11-04 00:00:00.000
Friday, 4 November 2022
More in: https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
You can do as follows
final DateFormat formatter = DateFormat('EEEE, dd MMMM yyyy');
final String formatted = formatter.format(DateTime.now());
Then you use the formatted

Grails dataBinding not working on default marshalled grails date

It seems that Grails by default is providing a date marshalled in JSON as "EEE MMM dd HH:mm:ss z yyyy" (e.g: Sat Jan 05 00:02:00 CET 2019)
If this (string) date is used in a domain creation activity (create an instance of the same domain class with this string-date as the date), Grails will not recognize it by default (although I think it should logically accept it).
Pooblem is that the Data Format "EEE MMM dd HH:mm:ss z yyyy" is not recognized in the
grails.databinding.dateFormats = [
'EEE MMM dd HH:mm:ss z yyyy',
'dd/MM/yyyy HH:mm:ss',
'dd/MM/yyyy',
'yyyy-MM-dd HH:mm:ss',
'yyyy-MM-dd HH:mm:ss.S',
"yyyy-MM-dd'T'hh:mm:ss'Z'",
'dd.mm.yyyy',
'MMddyyyy'
]
or in the #BindingFormat as well.
It looked like, to parse the 'EEE MMM dd HH:mm:ss z yyyy', the locale is also needed.
Something like:
def dateFormat = "EEE MMM dd HH:mm:ss z yyyy";
SimpleDateFormat smpDF= new SimpleDateFormat( dateFormat, Locale.UK );
Is there a way to pass the locate so grails binds this date as it is from the string?
Update: This is Grails 3.3.9

How to convert milliseconds to local day, date and time format in swift?

I want to display the date in this format (Wed Jan 10 2018 11:20:17). How to convert milliseconds to this format in swift?I want to get the day as Wed, time as 10:30 AM or PM and the date as 10 Jan.
First convert it in date by dividing it by 1000
var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
then use DateFormatter to convert in desired format you need
Note: Not tested in XCODE
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss"
print(dateFormatter.string(from: date))
Hope it is helpful to you.

New month date continues to count up

The new month of Feburary date continues to count up from January. So instead of showing Feburary 1St, it's showing Feburary 32 Like the picture below, any help would be appreciated thanks.
This is how I am getting the current date:
let date = Date()
let format = DateFormatter()
format.dateFormat = "EE, MMM DD, YYYY"
let currentDate = format.string(from: date)
header.headerTitle.text = currentDate
This is the result Feburary 32, 2018
Change "EE, MMM DD, YYYY" to "EE, MMM dd, yyyy" (or maybe just one d) and next time please try to read up on how date formatters work before trying to use them:
http://userguide.icu-project.org/formatparse/datetime

dateFromString() returns incorrect date

I'm trying to convert string to Date, but it result incorrect date.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY"
let dt = dateFormatter.dateFromString("17 Sep 2015")
println("Date : \(dt)")
It result
Date : Optional(2014-12-20 18:30:00 +0000)
Please let me know where I'm making mistake. I tried other format too, but it return nil.
The format for year is incorrect, it should be yyyy, not YYYY.
"Y": Year (in "Week of Year" based calendars). This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
See: Date Field SymbolTable.
Also: ICU Formatting Dates and Times
Your date format string is wrong. Change it to the following:
dateFormatter.dateFormat = "dd MMM yyyy"
For more information read Date Formatters documentation.