Datetime without 00:00:00.000 for flutter - flutter

Why everytime I make a
var date = DateTime.parse(dob);
var formattedDate = '${date.year}${date.month}${date.day}';
DateTime.parse(formattedDate)
it will still retrieve as 1961-02-26 00:00:00.000? How do I get it to become 19610226 but still in datatype DateTime?

Use DateFormat Class from intl package to format your date
var date = DateTime.parse(dob);
var dateFormatter = new DateFormat('yyyy-MM-dd');
String formattedDate = dateFormatter.format(date);
print(formattedDate); // result 2020-03-24
for more details and more formatting options see this answer

Related

Change this "2022-07-26T12:10:07.000+0000" format to DateTime with 12 hrs format? Flutter

How i can extract the Time alone in this format "2022-07-26T12:10:07.000+0000" and show in 12hrs.
The output i am supposed to get is "5:40 PM", but i am getting 12.10 AM.
How to get exact time in the above mentioned format?
This is the method i followed to parse the DateTime.
String getTimeStringNew(String date) {
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.parse(date);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(dateValue);
return value;}
In this method i am getting "12:10 AM".
But correct time is "5.40 PM".
What is the mistake i am doing here. Please correct.
2022-07-26T12:10:07.000+0000
Here 2022 is year
07 is month
26 is day
12 is hour
10 is minutes
07 is seconds.
So you will get 12.10 only
Maybe you are recieving the time in UTC.
You can use .toLocal to convert it to local time
var dateLocal = dateUtc.toLocal();
Edit
String getTimeStringNew(String date) {
DateTime _localDate = DateTime.parse(date).toLocal();
DateFormat dateFormat = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+SSSS");
var dateValue = dateFormat.format(_localDate);
DateFormat dateFormat1 = DateFormat("hh:mm a");
var value = dateFormat1.format(_localDate);
return value;}

How to convert a date to local date time

I cannot change my string date to local date in dart.
What am i missing.
this is my date format i get: "2021-02-01T17:00:00.000Z"
final dateTime = DateFormat("yyyy-MM-ddTHH:mm:ssZ").parse(date, true);
var dateLocal = dateTime.toLocal();
Use the DateTime.parse method:
String givenDate = '2021-02-01T17:00:00.000Z';
DateTime localDate = DateTime.parse(givenDate).toLocal();

Dart parse full dateTime including am/pm

I have my date here 2020/07/07 09:47:54 from my API in which I'm trying to parse to 2020/07/07 09:47am. I have tried the following method using intl package but I'm getting an error
DateFormat format = DateFormat('yyyy/MM/dd hh:mm').add_jm();
DateTime newDate = format.parse(date)
Unhandled Exception: FormatException: Trying to read from 2020/07/07 09:47:54 at position 20
Anyone has an idea what seems wrong here?
I think you need to add this line of code:
String date = DateFormat.yMEd().add_jms().format(DateTime.now());
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);
Solution
I found a way in which i need to parse the string date into a DateTime format then reformat it again based on what I needed.
DateTime dateTime = DateFormat('yyyy/MM/dd h:m').parse(date);
final DateFormat formatter = DateFormat('yyyy-MM-dd h:m a');
final String formatted = formatter.format(dateTime);
You can try formatting it twice like this:
String date = '2020/07/07 09:47:54';
DateFormat myDateFormat = DateFormat('yyyy/MM/dd hh:mm');
DateTime newDate = myDateFormat.parse(date);
var myString = myDateFormat.format(newDate) + DateFormat('a').format(newDate);

Read timestamp field and print as 12 December 2017 2:34:23

Initially, I had used string format for the field timestamp and I was able to convert it to 12 December 2017 2:34:23 format using toData().toString(). I need to do the same thing when the field timestamp is in timestamp format. Can I get some help on this?
You can get a DateTime variable from a timestamp using
var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp);
Once you have your date you can format however you want using a DateFormat for example:
var formatter = new DateFormat('yyyy-MM-dd'); // You can change the format here
String formatted = formatter.format(date);
print(formatted); // Something like 2019-04-20
just check out this example below :
var timestamp =1583772663;
var format = new DateFormat('dd MMMM yyyy, hh:mm:ss');
var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var value = format.format(date);
print(value);
//output : 09 March 2020, 10:21:03
let me know if it works.

Convert Date to Calendar in Groovy: .setTime() or .toCalendar?

I have always converted a Date into a Calendar using the setTime() method
Date date = new Date()
def calendar.setTime(date)
Recently I found the .toCalendar() method.
Date date = new Date()
def calendar = date.toCalendar()
What's the difference between them and which should I use?
As you can here this method does exactly the same:
public static Calendar toCalendar(Date self) {
Calendar cal = Calendar.getInstance();
cal.setTime(self);
return cal;
}