close

Converting TimeStamp in Flutter, Flutter Date time to Timestamp

Hello Guys. Many times we need Converting TimeStamp in Flutter, Flutter Date time to Timestamp. Here We have a tutorial to convert DateTime in different ways.

So Here I Have Share My Date Time Convert Class Which will convert your datetime formate for you.

How to Converting TimeStamp in Flutter, Flutter Date time to Timestamp?

  1. Converting TimeStamp in Flutter, Flutter Date time to Timestamp

    to Converting TimeStamp in Flutter, Flutter Date time to Timestamp You can also use use intl package and use my code.

  2. How to Converting TimeStamp in Flutter, Flutter Date time to Timestamp

    to Converting TimeStamp in Flutter, Flutter Date time to Timestamp You can also use use intl package and use my code.

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

Summery

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 Check Out Below Tutorials

1 thought on “Converting TimeStamp in Flutter, Flutter Date time to Timestamp”

Leave a Comment

%d bloggers like this: