alibaba-aero / jalaliday

Persian (Jalali, Khorshidi) Plugin for Day.js
MIT License
135 stars 21 forks source link

Computational mistake in converting dates #5

Closed HamedFathi closed 5 years ago

HamedFathi commented 5 years ago

Please consider the sample:

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

dayjs.extend(jalaliday);
dayjs.calendar('jalali');

const date = dayjs('1367/02/23', { jalali: true })
const gregory = date.calendar('gregory')
const jalaliDate = date.calendar('jalali');

// Should be 1367/02/23, but is 1367/1/23
console.log(jalaliDate.year() + "/" + jalaliDate.month() +"/"+ jalaliDate.date()); 

 // Should be 1988/05/13, but is 1988/4/13
console.log(gregory.year() + "/" + gregory.month() +"/"+ gregory.date());

This library calculates the conversion of dates incorrectly.

farnabaz commented 5 years ago

There is no issue in your sample, In Dayjs month indexes start from zero, so the index of 5th month is 4
If you want the exact month index, consider using format function

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

dayjs.extend(jalaliday);
dayjs.calendar('jalali');

const date = dayjs('1367/02/23', { jalali: true })
const gregory = date.calendar('gregory')
const jalaliDate = date.calendar('jalali');

// Should be 1367/02/23, but is 1367/1/23
console.log(jalaliDate.year() + "/" + jalaliDate.month() +"/"+ jalaliDate.date());
// exact match
console.log(jalaliDate.format('YYYY/MM/DD')); // 1367/02/23

 // Should be 1988/05/13, but is 1988/4/13
console.log(gregory.year() + "/" + gregory.month() +"/"+ gregory.date());
// exact match
console.log(gregory.format('YYYY/MM/DD')); // 1988/05/13