数据可视化大屏
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import moment from 'moment';
  2. export class AppUtils {
  3. public static dateFormat(date: Date, format: string): any {
  4. if (!date) {
  5. return null;
  6. }
  7. return moment(date).format(format);
  8. }
  9. public static convertDate(str: string, format?: string, substring?: boolean) {
  10. if (str) {
  11. const data = moment(substring && format ? str.substring(0, format.length) : str, format);
  12. if (data.isValid()) {
  13. return data.toDate();
  14. }
  15. }
  16. return null;
  17. }
  18. public static calAge(birth: string | Date, format?: string) {
  19. let birthday;
  20. if (typeof birth == 'string') {
  21. birthday = this.convertDate(birth, format);
  22. } else {
  23. birthday = birth;
  24. }
  25. if (birthday) {
  26. const now = new Date();
  27. let age = now.getFullYear() - birthday.getFullYear();
  28. if (now.getMonth() < birthday.getMonth() || (now.getMonth() == birthday.getMonth() && now.getDay() < birthday.getDay())) {
  29. age--;
  30. }
  31. return age < 0 ? 0 : age;
  32. }
  33. return null;
  34. }
  35. public static isNull(val: any) {
  36. if (val === null || val === undefined || val === '') {
  37. return true;
  38. }
  39. return false;
  40. }
  41. public static convertToCheck(val: any) {
  42. if (val) {
  43. return '√';
  44. }
  45. return '';
  46. }
  47. public static convertToBool(val: any) {
  48. if (val) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. public static boolConvertToNumber(val: any) {
  54. if (val) {
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. public static convertEnumToList(obj: any) {
  60. const list: any = [];
  61. if (obj) {
  62. for (const key in obj) {
  63. const val = obj[key];
  64. list.push({ value: Number.parseInt(key), label: val });
  65. }
  66. }
  67. return list;
  68. }
  69. }