Hello Guys, How are you all? Hope you all are fine. In today’s tutorial, we are going to learn How to convert Timestamp in a flutter? Many time we need to convert our timestamp Or our date time formate to another formate so here is I have all possible Methods to convert timestamp in a flutter.
So Here I Have Share My Date Time Convert Class Which will convert your datetime formate for you.
How to convert TimeStamp in a flutter?
- How to convert TimeStamp in a flutter
to convert TimeStamp in a flutter Many times we need to convert our timestamp or our date time formate to another format so here I have added all possible methods to convert timestamp in flutter.
- How to convert Timestamp to DateTime from firebase with flutter ?
to convert Timestamp to DateTime from firebase with flutter
Timestamp
class has atoDate
function that converts it to aDateTime
object. See this for more information. Any formatting you want to do in converting it to a string can be done more easily now withintl
package formatters.
Method 1
String readTimestamp(int timestamp) {
var now = DateTime.now();
var format = DateFormat('HH:mm a');
var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var diff = now.difference(date);
var time = '';
if (diff.inSeconds <= 0 || diff.inSeconds > 0 && diff.inMinutes == 0 || diff.inMinutes > 0 && diff.inHours == 0 || diff.inHours > 0 && diff.inDays == 0) {
time = format.format(date);
} else if (diff.inDays > 0 && diff.inDays < 7) {
if (diff.inDays == 1) {
time = diff.inDays.toString() + ' DAY AGO';
} else {
time = diff.inDays.toString() + ' DAYS AGO';
}
} else {
if (diff.inDays == 7) {
time = (diff.inDays / 7).floor().toString() + ' WEEK AGO';
} else {
time = (diff.inDays / 7).floor().toString() + ' WEEKS AGO';
}
}
return time;
}
Method 2
import 'package:intl/intl.dart';
DateTime convertTimeStampToDateTime(int timeStamp) {
var dateToTimeStamp = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000);
return dateToTimeStamp;
}
String convertTimeStampToHumanDate(int timeStamp) {
var dateToTimeStamp = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000);
return DateFormat('dd/MM/yyyy').format(dateToTimeStamp);
}
String convertTimeStampToHumanHour(int timeStamp) {
var dateToTimeStamp = DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000);
return DateFormat('HH:mm').format(dateToTimeStamp);
}
int constructDateAndHourRdvToTimeStamp(DateTime dateTime, TimeOfDay time ) {
final constructDateTimeRdv = dateTimeToTimeStamp(DateTime(dateTime.year, dateTime.month, dateTime.day, time.hour, time.minute)) ;
return constructDateTimeRdv;
}
Method 3
You can also use use intl package.
import 'package:intl/intl.dart';
int timeInMillis = 1586348737122;
var date = DateTime.fromMillisecondsSinceEpoch(timeInMillis);
var formattedDate = DateFormat.yMMMd().format(date); // Apr 8, 2020
So it’s all About Converting TimeStamp in Flutter, Flutter Date time to Timestamp. Hope this tutorial helped you a lot. Comment below Your thoughts and your queries. And Also Comment on your suggestion here.
Also Read
- Error: Struct ‘Utf8’ is empty. Support for empty structs is deprecated… Use Opaque instead in flutter
- Error: Only static members can be accessed in initializers
- AnimationController The named parameter ‘vsync’ isn’t defined
- Flutter App stuck at “Running Gradle task ‘assembleDebug’… ”
- Error: No named parameter with the name ‘keyboardDismissBehavior’
Leave a Reply