mistake to convert date time in angular with moment library in angular

553 views Asked by At

When I want to convert the Gregorian date to Persian date, it converts the value of the minute in the date conversion to error.

For example I want to convert this date time to Persian date:

2020-09-14T16:51:00+04:30 must convert to this 1399/06/24 16:51 but when convert date it show me this time 1399/06/24 00:06 it mistake to convert 16:51, show this: 00:06.

This is my code to convert date:

toPersianDate(date: any, format = 'YYYY/MM/DD HH:MM'): string {
    let dateTime;
    const MomentDate = moment(date, 'YYYY/MM/DD');
    dateTime = MomentDate.locale('fa').format('jYYYY/jMM/jDD HH:jMM');
    return dateTime;
}

What's the problem? How can I solve this problem?

1

There are 1 answers

0
yazantahhan On

the MM is used for month formatting, so it is trying to format the minutes into a month.

What you need to use is the small mm. Moreover, I don't this you need the j before the mm as the minutes are the same in Jalali time.

So what you actually need is this: MomentDate.locale('fa').format('jYYYY/jMM/jDD HH:mm');

You can read more about the formatting here.