Finap Datetime Helper

Features

Utility functions for Date Time

Getting started

Run this command to install

flutter pub add finap_datetime_helper

Usage

Get now as Miliseconds

static int getNowFromMilliseconds() {
    try {
      return DateTime.now().toUtc().millisecondsSinceEpoch;
    } on Exception catch (e) {
      print(e.toString());
      return 0;
    }
  }

get Date time from miliseconds

static DateTime? getDate(int miliseconds) {
    try {
      return DateTime.fromMillisecondsSinceEpoch(miliseconds, isUtc: true);
    } on Exception catch (e) {
      print(e.toString());
      return null;
    }
  }

get string date from miliseconds

static String? getDateString(int? miliseconds) {
    final formatter = DateFormat('dd-MMM-yyyy hh:mm a');
    if (miliseconds == null) return '';
    final date = getDate(miliseconds);
    if (date != null) {
      return formatter.format(date.toLocal());
    }
    return '';
  }

get Date in seconds

static String? getDateInSecondsString(int? seconds) {
    final formatter = DateFormat('dd-MMM-yyyy hh:mm a');
    if (seconds == null) return '';
    final date = getDate(seconds * 1000);
    if (date != null) {
      return formatter.format(date.toLocal());
    }
    return '';
  }

get Only date string by providing miliseconds

static String? getOnlyDateString(int? miliseconds) {
    final formatter = DateFormat('dd-MMM-yyyy');
    if (miliseconds == null) return '';
    final date = getDate(miliseconds);
    if (date != null) {
      return formatter.format(date.toLocal());
    }
    return '';
  }