You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3KB

  1. import 'dart:developer';
  2. import 'package:intl/intl.dart';
  3. extension ddMM_HHmm on String {
  4. //"2020-08-13T02:11:32Z" => 13/08 02:11
  5. // convert utc to local timezone
  6. String format_DDMM_HHmm() {
  7. try {
  8. final str = this.toString();
  9. var dateFromString =
  10. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(str, true).toLocal();
  11. return DateFormat("dd/MM HH:mm").format(dateFromString);
  12. } catch (_) {
  13. return "";
  14. }
  15. }
  16. //TODO: Check timezone with api update true/false
  17. String format_DDMMYY_HHmm() {
  18. try {
  19. final str = this.toString();
  20. var dateFromString =
  21. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(str, false).toLocal();
  22. return DateFormat("dd/MM/yyyy HH:mm").format(dateFromString);
  23. } catch (_) {
  24. return "";
  25. }
  26. }
  27. }
  28. extension numToString on num {
  29. String formatNumtoStringDecimal() {
  30. try {
  31. var numWithLocalSeparator = new NumberFormat.decimalPattern("vi_VN");
  32. final str = numWithLocalSeparator.format(this);
  33. return str;
  34. } catch (_) {
  35. return "";
  36. }
  37. }
  38. }
  39. extension HHmm on Duration {
  40. String formatHHmm() {
  41. //1:34:00.000000
  42. final str = this.toString();
  43. final texts = str.split(":");
  44. final textHour = texts[0].padLeft(2, '0');
  45. final textMinute = texts[1].padLeft(2, '0');
  46. return "${textHour}h ${textMinute}m";
  47. }
  48. }
  49. extension FormatNumber on int {
  50. String formatDecimalThousand() {
  51. //1403 -> 1,403
  52. var f = new NumberFormat.decimalPattern("vi_VN");
  53. return f.format(this);
  54. }
  55. }
  56. extension FormatNumberToDecimal on double {
  57. String formatDecimalThousand() {
  58. var f = new NumberFormat.decimalPattern("vi_VN");
  59. return f.format(this);
  60. }
  61. }
  62. extension FormatDate on int {
  63. String MMM_dd_yyyy() {
  64. return DateFormat("MMM dd, yyyy")
  65. .format(DateTime.fromMillisecondsSinceEpoch(this * 1000));
  66. }
  67. }
  68. extension DoubleParsing on String {
  69. double parseDoubleThousand() {
  70. var newValue = this.replaceAll(".", "");
  71. var changeToServerFormat = newValue.replaceAll(",", ".");
  72. return double.tryParse(changeToServerFormat);
  73. }
  74. }
  75. extension IntParsing on String {
  76. int parseIntThousand() {
  77. var newValue = this.replaceAll(".", "");
  78. var changeToServerFormat = newValue.replaceAll(",", ".");
  79. return int.tryParse(changeToServerFormat);
  80. }
  81. }