Masir
Masir

Reputation: 337

get the Days of persian Month in dayjs package

i write a function for getting days of month . i use dayjs and jalaliday for build this function .

My code:

import dayjs from "dayjs";
import jalaliday from 'jalaliday'

dayjs.extend(jalaliday)
dayjs.calendar('jalali').locale('fa')
export function GetMonth(month =dayjs().month()){
    const year = dayjs().year();
    
    const FirstDayOfTheMonth= dayjs(new Date(year ,month,1)).calendar('jalali').day();
    let CurrentMonthCount =0 -FirstDayOfTheMonth;
    const DaysMatrix =new Array(5).fill([]).map(()=>{
        return new Array(7).fill(null).map(()=>{
                CurrentMonthCount++
                return dayjs(new Date(year,month,CurrentMonthCount)).calendar('jalali').locale('fa')
        })
    });
    return DaysMatrix; 
}

i use this function in my react component and, when i log this function it get's me english date like this :

my consol.table(GetMonth())

only the year is correct. how can i get persian date details.

Upvotes: 2

Views: 1599

Answers (1)

Mohsen.RJ
Mohsen.RJ

Reputation: 384

try this Man :

import dayjs from "dayjs";
import jalaliday from 'jalaliday'
dayjs.extend(jalaliday)
export function GetMonth(month = dayjs().month()+1) {
    const year = dayjs().year();

    const FirstDayOfTheMonth = dayjs(new Date(year, month, 1)).day();
    let CurrentMonthCount = 0 - FirstDayOfTheMonth;
    const DaysMatrix = new Array(5).fill([]).map(() => {
        return new Array(7).fill(null).map(() => {
            CurrentMonthCount++
            return dayjs(new Date(year,month,CurrentMonthCount)).calendar('jalali').locale('fa').format('DD MMMM YYYY')
        })
    });
    return DaysMatrix
}

Upvotes: 3

Related Questions