Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as moment from 'jalali-moment';
- export class DateHelper {
- static monthsNames = [
- 'فروردین',
- 'اردیبهشت',
- 'خرداد',
- 'تیر',
- 'مرداد',
- 'شهریور',
- 'مهر',
- 'آبان',
- 'آذر',
- 'دی',
- 'بهمن',
- 'اسفند'
- ];
- static convert(value: Date | string, withTime: boolean = true): string {
- let str: string;
- let result: string;
- if (!value || value.toString().length === 0) {
- value = new Date();
- }
- if (typeof (value) !== 'string') {
- str = `${value.getFullYear()}-${value.getMonth() + 1}-${value.getDate()}`;
- if (withTime) {
- str += ` ${value.getHours()}:${value.getMinutes()}:${value.getSeconds()}`;
- }
- } else {
- str = value;
- }
- if (withTime) {
- result = this.fullConvert(str);
- } else {
- result = this.halfConvert(str);
- }
- return result;
- }
- static convertMonthAndYearForCalendar(value: Date | string) {
- let str: string;
- let result: string;
- if (value) {
- if (typeof (value) !== 'string') {
- str = `${value.getFullYear()}-${value.getMonth() + 1}-${value.getDate()}`;
- }
- const momentDate = moment(str, 'YYYY-MM-DD');
- result = momentDate.locale('fa').format('MMM jYYYY');
- }
- return result;
- }
- private static fullConvert(str: string): string {
- const momentDate = moment(str, 'YYYY-MM-DD HH:mm:ss');
- return momentDate.locale('fa').format('dddd، DD MMM jYYYY HH:mm:ss');
- }
- private static halfConvert(str: string): string {
- const momentDate = moment(str, 'YYYY-MM-DD');
- return momentDate.locale('fa').format('dddd، DD MMM jYYYY');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement