You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
doctor_app_flutter/lib/util/date-utils.dart

239 lines
6.4 KiB
Dart

import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
class DateUtils {
static String convertDateToFormat(DateTime dateTime, String dateFormat) {
return DateFormat(dateFormat).format(dateTime);
}
static String convertStringToDateFormat(String date, String dateFormat) {
DateTime dateTime = DateTime.parse(date);
return convertDateToFormat(dateTime, dateFormat);
}
static convertDateFromServerFormat(String str, dateFormat) {
var date = getDateTimeFromServerFormat(str);
return DateFormat(dateFormat).format(date);
}
static DateTime getDateTimeFromServerFormat(String str) {
const start = "/Date(";
const end = "+0300)";
final startIndex = str.indexOf(start);
final endIndex = str.indexOf(end, startIndex + start.length);
var date = new DateTime.fromMillisecondsSinceEpoch(
int.parse(str.substring(startIndex + start.length, endIndex)));
return date;
}
static String differenceBetweenDateAndCurrentInYearMonthDay(DateTime firstDate, BuildContext context) {
DateTime now = DateTime.now();
// now = now.add(Duration(days: 400, minutes: 0));
var difference = firstDate.difference(now);
int years = now.year - firstDate.year;
int months = now.month - firstDate.month;
int days = now.day - firstDate.day;
if (months < 0 || (months == 0 && days < 0)) {
years--;
months += (days < 0 ? 11 : 12);
}
if (days < 0) {
final monthAgo = new DateTime(now.year, now.month - 1, firstDate.day);
days = now.difference(monthAgo).inDays + 1;
}
return "$days ${TranslationBase.of(context).days}, $months ${TranslationBase.of(context).months}, $years ${TranslationBase.of(context).years}";
}
static String differenceBetweenDateAndCurrent(DateTime firstDate, BuildContext context) {
DateTime now = DateTime.now();
// DateTime now = nows.add(Duration(days: 400, minutes: 25, hours: 0));
var difference = now.difference(firstDate);
int minutesInDays = difference.inMinutes;
int hoursInDays = minutesInDays ~/ 60; // ~/ : truncating division to make the result int
int minutes = minutesInDays % 60;
int days = hoursInDays ~/ 24;
int hours = hoursInDays % 24;
double hoursInOneDay = difference.inHours / difference.inDays;
return "$days ${TranslationBase.of(context).days}, $hours ${TranslationBase.of(context).hr}, $minutes ${TranslationBase.of(context).min}";
}
static String differenceBetweenServerDateAndCurrent(String str, BuildContext context) {
const start = "/Date(";
const end = "+0300)";
final startIndex = str.indexOf(start);
final endIndex = str.indexOf(end, startIndex + start.length);
var date = new DateTime.fromMillisecondsSinceEpoch(
int.parse(str.substring(startIndex + start.length, endIndex)));
return differenceBetweenDateAndCurrent(date, context);
}
/// get month by
/// [weekDay] convert week day in int to week day name
static getWeekDay(int weekDay) {
switch (weekDay) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday ";
case 7:
return "Sunday";
}
}
/// get month by
/// [weekDay] convert week day in int to week day name arabic
static getWeekDayArabic(int weekDay) {
switch (weekDay) {
case 1:
return "الاثنين";
case 2:
return "الثلاثاء";
case 3:
return "الاربعاء";
case 4:
return "الخميس";
case 5:
return "الجمعه";
case 6:
return "السبت ";
case 7:
return "الاحد";
}
}
/// get month by
/// [month] convert month number in to month name
static getMonth(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
}
}
/// get month by
/// [month] convert month number in to month name in Arabic
static getMonthArabic(int month) {
switch (month) {
case 1:
return "يناير";
case 2:
return " فبراير";
case 3:
return "مارس";
case 4:
return "أبريل";
case 5:
return "مايو";
case 6:
return "يونيو";
case 7:
return "يوليو";
case 8:
return "أغسطس";
case 9:
return "سبتمبر";
case 10:
return " اكتوبر";
case 11:
return " نوفمبر";
case 12:
return "ديسمبر";
}
}
static getMonthByName(String month) {
switch (month.toLowerCase()) {
case 'january':
return 1;
case 'february':
return 2;
case 'march':
return 3;
case 'april':
return 4;
case 'may':
return 5;
case 'june':
return 6;
case 'july':
return 7;
case 'august':
return 8;
case 'september':
return 9;
case 'october':
return 10;
case 'november':
return 11;
case 'december':
return 12;
}
}
static String getAgeByBirthday(dynamic birthday, BuildContext context){
// https://leechy.dev/calculate-dates-diff-in-dart
DateTime birthDate = DateUtils.getDateTimeFromServerFormat(birthday);
final now = DateTime.now();
int years = now.year - birthDate .year;
int months = now.month - birthDate.month;
int days = now.day - birthDate.day;
if (months < 0 || (months == 0 && days < 0)) {
years--;
months += (days < 0 ? 11 : 12);
}
if (days < 0) {
final monthAgo = new DateTime(now.year, now.month - 1, birthDate.day);
days = now.difference(monthAgo).inDays + 1;
}
return "$years ${TranslationBase.of(context).years} $months ${TranslationBase.of(context).months} $days ${TranslationBase.of(context).days}";
}
}