Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import * as moment from 'jalali-moment';
  2.  
  3. export class DateHelper {
  4. static monthsNames = [
  5. 'فروردین',
  6. 'اردیبهشت',
  7. 'خرداد',
  8. 'تیر',
  9. 'مرداد',
  10. 'شهریور',
  11. 'مهر',
  12. 'آبان',
  13. 'آذر',
  14. 'دی',
  15. 'بهمن',
  16. 'اسفند'
  17. ];
  18.  
  19. static convert(value: Date | string, withTime: boolean = true): string {
  20. let str: string;
  21. let result: string;
  22.  
  23. if (!value || value.toString().length === 0) {
  24. value = new Date();
  25. }
  26.  
  27. if (typeof (value) !== 'string') {
  28. str = `${value.getFullYear()}-${value.getMonth() + 1}-${value.getDate()}`;
  29. if (withTime) {
  30. str += ` ${value.getHours()}:${value.getMinutes()}:${value.getSeconds()}`;
  31. }
  32. } else {
  33. str = value;
  34. }
  35.  
  36. if (withTime) {
  37. result = this.fullConvert(str);
  38. } else {
  39. result = this.halfConvert(str);
  40. }
  41.  
  42. return result;
  43. }
  44.  
  45. static convertMonthAndYearForCalendar(value: Date | string) {
  46. let str: string;
  47. let result: string;
  48.  
  49. if (value) {
  50. if (typeof (value) !== 'string') {
  51. str = `${value.getFullYear()}-${value.getMonth() + 1}-${value.getDate()}`;
  52. }
  53.  
  54. const momentDate = moment(str, 'YYYY-MM-DD');
  55. result = momentDate.locale('fa').format('MMM jYYYY');
  56. }
  57.  
  58. return result;
  59.  
  60. }
  61.  
  62. private static fullConvert(str: string): string {
  63. const momentDate = moment(str, 'YYYY-MM-DD HH:mm:ss');
  64. return momentDate.locale('fa').format('dddd، DD MMM jYYYY HH:mm:ss');
  65. }
  66.  
  67. private static halfConvert(str: string): string {
  68. const momentDate = moment(str, 'YYYY-MM-DD');
  69. return momentDate.locale('fa').format('dddd، DD MMM jYYYY');
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement