How to show time format - flutter

I want to show only its time without date, so that my chart can be displayed perfectly. The problem is DateFormat that I used is not properly like below:
with the code like this:
xValueMapper: (GraphicLoadUnload data, _) => data.startDate.toString()
while I want the date only shows time like this:

import 'package:intl/intl.dart';
DateTime now = DateTime.now(); //Set your datetime value
String formattedDate = DateFormat('kk:mm').format(now);

Related

How to convert to readable dates in Dart

I would like to know how a date such as "2022-07-17T01:46:12.632892+05:30" be converted to a Human Readable date in DD/MM/YYYY and hh:mm:ss format? I probably have not surfed through a lot of other questions and suggestions on the Internet but the ones I came across were not of any help. Also, what are such date formats(like the one in question) called?
It is rather straightforward using DateFormat from the package intl which comes with Flutter:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime)); // 16/07/2022 20:16:12
}
The time has here been converted to UTC to make the example the same for all readers. If not, the created DateTime would be localtime which uses the timezone on the device which the program are running.
If you want to print the time using the timezone offset of 5 hours and 30 minutes, you can do something like this:
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.parse('2022-07-17T01:46:12.632892+05:30').toUtc();
final dateFormat = DateFormat('dd/MM/yyyy HH:mm:ss');
print(dateFormat.format(dateTime.add(const Duration(hours: 5, minutes: 30))));
// 17/07/2022 01:46:12
}

How to remove offset in datetime

i would like some help on this i actually use TZDateTime and also with specific location now i would to remove the end of the result like .123436+100 from it
final dateTime = TZDateTime.now(getLocation(box.read(timezone)));
the result always i get is something like this
2022-04-20 17:42:05.032173+1000
but i want is only like this
2022-04-20 17:42:05
how can i remove that actually
I haven't used TZDateTime before but I think it's the same as DateTime. By using DateFormat from intl you can format the string as you want.
ps1: in case TZDateTime cant parse directly, you can turn it to string and pass to DateTime.parse.
ps2: this answer of mine is an idea and has not been tested correctly, sorry if it didn't help you.
import 'package:intl/intl.dart';
void main() {
var time = DateTime.parse('2022-04-20 17:42:05.032173+1000');
var fm = DateFormat('yyyy-MM-dd hh:mm:ss');
print(fm.format(time)); // -> 2022-04-20 07:42:05
}

Flutter how to convert timestamp date time

I am getting date and time from store. In data base its look like this
Need to know how can I show this as DD/MM/YY
I am trying to do like this
String timeString = snapshot.data[index]['lastupdate'].toString();
DateTime date = DateTime.parse(timeString);
print(DateFormat('yyyy-MM-dd').format(date));
Its showing error of Invalid date format
Try like this your lastupdate date is not convertime inDate thats why its showing error
DateTime date = DateTime.parse(snapshot.data[index]['lastupdate'].toDate().toString());
print(DateFormat('dd-MMM-yyy').format(date));
Use function to get date from Timestamp like this:
readDate(Timestamp dateTime) {
DateTime date = DateTime.parse(dateTime.toDate().toString());
// add DateFormat What you want. Look at the below comment example
//String formatedDate = DateFormat('dd-MMM-yyy').format(date);
String formatedDate = DateFormat.yMMMMd().format(date);
return formatedDate;
}
Use the function when you want it.
For example:
Text(readDOB(streamSnapshot.data!["dob"]))
For this you should install intl package. read

Flutter get current Date format style

I searched a lot without finding a solution.
I would like to get the current Date time format by locale (https://en.wikipedia.org/wiki/Date_format_by_country)
For example, if my locale zone in the U.S. I would like to get "MDY" if Europe "DMY" and so on.
Is there a way to get this?
You can use the intl package and try this:
DateFormat.yMd(Localizations.localeOf(context).languageCode).format(date);
Just simply give the formate style and then fetch it..
DateFormat("dd/MMM/yyyy").format(date);
intl is not enough.
intl will only format a DateTime with a given date format String such as dd/MMM/yyyy.
In order to get users preferred date format String
you can use system_date_format
Here is an example of how to combine intl with system_date_format to achieve your goal.
final date = DateTime.now();
final dateFormatString = SystemDateTimeFormat().dateFormat;
print(dateFormatString ); // e.g. "M/d/yy"
final formattedDateString = DateFormat(dateFormatString).format(date);
print(formattedDateString ); // "2/14/23"
You could use the dateTimeSymbolMap:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/date_symbols.dart';
void main(List<String> args) {
var dateTimeSymbolMap2 = dateTimeSymbolMap()['en'] as DateSymbols;
print(dateTimeSymbolMap2.DATEFORMATS); // prints [EEEE, MMMM d, y, MMMM d, y, MMM d, y, M/d/yy]
}

Convert epoch time into timestamp flutter

I have a date-time in epoch format: 1633247247
I want to convert it into timestamps like this: Sunday, 3 October 2021 or just October 3 2021
I am writing this code
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247);
But it is returning 1970-01-19 18:25:11.247
Edit I ran this code
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
Got the output in datetime. I am now trying to convert into string
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(date);
It gives this error The instance member 'date' can't be accessed in an initializer.
It represent to milliseconds, you need to multiple it with 1000 like below :
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
It seems like that the time you've is 'seconds since epoch' so just multiplying by 1000 should give you the correct time.
final DateTime timeStamp = DateTime.fromMillisecondsSinceEpoch(1633247247 * 1000);
Download and Import
intl Pacakge
import 'package:intl/intl.dart';
Initialize a variable
String datetime;
Get Current date and conversion
Future<void> Datetimes() async{
setState(() {
DateTime now = DateTime.now();
String formattedDate = DateFormat('EEEE d MMM hh:mm:ss').format(now);
datetime=formattedDate;
});
}
Output
Saturday 3 oct 2:07:29
Refer this for DateandTime class for more formatting parameters
DateFormat-class
You need to use DateTime Formats. You can find a solution here:
https://stackoverflow.com/a/51579740/11604909