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.

93 lines
2.2KB

  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. String format_DDMMYY_HHmm() {
  17. try {
  18. final str = this.toString();
  19. var dateFromString =
  20. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(str, true).toLocal();
  21. return DateFormat("dd/MM/yyyy HH:mm").format(dateFromString);
  22. } catch (_) {
  23. return "";
  24. }
  25. }
  26. }
  27. extension numToString on num {
  28. String formatNumtoStringDecimal() {
  29. try {
  30. var numWithLocalSeparator = new NumberFormat.decimalPattern("vi_VN");
  31. final str = numWithLocalSeparator.format(this);
  32. return str;
  33. } catch (_) {
  34. return "";
  35. }
  36. }
  37. }
  38. extension HHmm on Duration {
  39. String formatHHmm() {
  40. //1:34:00.000000
  41. final str = this.toString();
  42. final texts = str.split(":");
  43. final textHour = texts[0].padLeft(2, '0');
  44. final textMinute = texts[1].padLeft(2, '0');
  45. return "${textHour}h ${textMinute}m";
  46. }
  47. }
  48. extension FormatNumber on int {
  49. String formatDecimalThousand() {
  50. //1403 -> 1,403
  51. var f = new NumberFormat.decimalPattern("vi_VN");
  52. return f.format(this);
  53. }
  54. }
  55. extension FormatNumberToDecimal on double {
  56. String formatDecimalThousand() {
  57. var f = new NumberFormat.decimalPattern("vi_VN");
  58. return f.format(this);
  59. }
  60. }
  61. extension FormatDate on int {
  62. String MMM_dd_yyyy() {
  63. return DateFormat("MMM dd, yyyy")
  64. .format(DateTime.fromMillisecondsSinceEpoch(this * 1000));
  65. }
  66. }
  67. extension DoubleParsing on String {
  68. double parseDoubleThousand() {
  69. var newValue = this.replaceAll(".", "");
  70. var changeToServerFormat = newValue.replaceAll(",", ".");
  71. return double.tryParse(changeToServerFormat);
  72. }
  73. }
  74. extension IntParsing on String {
  75. int parseIntThousand() {
  76. var newValue = this.replaceAll(".", "");
  77. var changeToServerFormat = newValue.replaceAll(",", ".");
  78. return int.tryParse(changeToServerFormat);
  79. }
  80. }