Date format for the following dates? [duplicate] - flutter

This question already has answers here:
How do I format a date with Dart?
(24 answers)
Closed 4 months ago.
I want to show a date time in the following format 01-Mar-2000 10:10:10 PM/AM.
How to achieve it?
Please suggest a one line answer to write optimized code.

You can use intl package DateFormat.
The format will be DateFormat("dd-MM-yyyy h:mm:ss a");
single line will be
DateFormat("dd-MM-yyyy h:mm:ss a").format(dateTime);
final dateTime = DateTime.now();
DateFormat formater = DateFormat("dd-MM-yyyy h:mm:ss a");
final data = formater.format(dateTime);
print(data);//30-10-2022 4:14:19 PM

Related

Convert date and time in dart [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
How do I format a date with Dart?
(24 answers)
Closed 9 months ago.
from API I am getting this:
"2022-05-12T15:55:03.000000Z"
My question is, how can I make it:
12.05.2022, 15:55
My question is, is here any easy way? Otherwise I can slowly and partially convert it with string methods, but I guess there must be better, more proper way?
Thank you in advance
you can user DateFormat to get a date as string formatted the way you want
String x = "2022-05-12T15:55:03.000000Z";
DateTime y = DateTime.parse(x);
final DateFormat formatter = DateFormat('dd.MM.yyyy, HH:mm');
final String formatted = formatter.format(y);
print(formatted); // 12.05.2022, 15:55

Swift 5 - Dateformatter not working as expected [duplicate]

This question already has answers here:
Swift - Get local date and time
(11 answers)
Closed 1 year ago.
I was playing around with date formatter in swift, but the AM/PM thing is not working in my code.
import Foundation
let dtstr = "Tuesday, July 28, 2020 4:15:45 PM"
let formatter = DateFormatter()
formatter.dateFormat = "eeee, MMMM d, yyyy h:m:s a"
let date = formatter.date(from: dtstr)
print(date)
the output is this: Optional(2020-07-28 08:15:45 +0000). However, it should be 16:15:45 instead of this. Any idea why?
Thanks!
Date has no information about time zone, and default string representation is using a greenwich one. You can see it +0000 part in your string.
You can get description for your own time zone like this:
date.description(with: .current)

How can I work write with dates in Swift 4? [duplicate]

This question already has answers here:
Calculate time interval to 0.00 of the next day according to GMT in swift or objective-c?
(2 answers)
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 4 years ago.
I have a task for working with dates. The meaning of a task is to create a date in ISO format. I can do this:
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
formatter.timeZone = TimeZone.current
formatter.locale = Locale(identifier: "ru_RU")
Well, the result of function is 2018-02-25T15:51:51+03:00
How can I output the beginning of a next day in this format?
I tried to deal with dataComponents but I failed.
I am a beginner in Swift 4 development and I need your help, please.

swift stored date turns different when formatted to string [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 7 years ago.
I stored a date in core data (as a date), and with the println it shows correctly its value: april 21 (is the var dateX below), but when right after the println i format it to string with the following code, the label linked to it shows april 22 (which is today, so i wonder tomorrow will show 23 etc.), where is the problem? anyone?
thank you
if dateX != nil{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy"
dateFormatter.timeZone = NSTimeZone.defaultTimeZone()
var dateXstring = dateFormatter.stringFromDate(dateX as NSDate)
startLabel.text = "Profile created on \(dateXstring)"
}
println dateX and dateXstring:
my time zone is Rome (Italy)
You likely have a timezone issue. Where are you located? DefaultTimeZone could be GMT/ZULU time which is -5 hrs from the east coast.
A good way to check is to use the timeIntervalSince1970 function (i think thats what it is called). If the stored date and retrieved date have the same value its the same date and you have a display problem.
timeIntervalSince1970 returns a NSTimeInterval which is really a Double

GWT DateTimeFormat returns the wrong date

I have the following code
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
And the alert box shows the date
09/04/2014
instead of
21/04/2013
Why?
As others already say, it's because of your pattern. What they don't say is why it behaves that way.
When parsing 21/04/2013 as MM/dd/yyyy, DateTimeFormat will decompose the date as:
Month Day of month Year
21 4 2013
and it'll then adjust things to make a valid date. To do that, the Month part is truncated at 12 (so that temporary date is Dec 4th, 2013) and the remainder (21 - 12 = 9) is then added, leading to Sept. 4th 2014, which according to your format displays as 09/04/2014.
You wanted to show 21/04/2013 but the format was MM/dd/yyyy.
It should be dd/MM/yyyy
So change it like this:
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
You're reversing day and month.
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));