最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert gregorian date to jalali - Stack Overflow

programmeradmin1浏览0评论

I have a function like this:

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var hdr = {};

and there is another function to convert it:

function gregorian_to_jalali($g_y,$g_m,$g_d,$mod=''){
    $g_y=tr_num($g_y); $g_m=tr_num($g_m); $g_d=tr_num($g_d);
 $d_4=$g_y%4;
 $g_a=array(0,0,31,59,90,120,151,181,212,243,273,304,334);
 $doy_g=$g_a[(int)$g_m]+$g_d;
 if($d_4==0 and $g_m>2)$doy_g++;
 $d_33=(int)((($g_y-16)%132)*.0305);
 $a=($d_33==3 or $d_33<($d_4-1) or $d_4==0)?286:287;
 $b=(($d_33==1 or $d_33==2) and ($d_33==$d_4 or $d_4==1))?78:(($d_33==3 and $d_4==0)?80:79);
 if((int)(($g_y-10)/63)==30){$a--;$b++;}
 if($doy_g>$b){
  $jy=$g_y-621; $doy_j=$doy_g-$b;
 }else{
  $jy=$g_y-622; $doy_j=$doy_g+$a;
 }
 if($doy_j<187){
  $jm=(int)(($doy_j-1)/31); $jd=$doy_j-(31*$jm++);
 }else{
  $jm=(int)(($doy_j-187)/30); $jd=$doy_j-186-($jm*30); $jm+=7;
 }
 return($mod=='')?array($jy,$jm,$jd):$jy.$mod.$jm.$mod.$jd;
}

I'm just confused how to use this functions to convert that hdr date to jalali date? I tried this but didn't work:

1

var date = new jdate();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var hdr = {};

2 also this:

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var hdr = gregorian_to_jalali(y,m,d);

//var hdr = {};

What do I do wrong?

and what this line do: var hdr = {}; ?

let me explain whole of my work, I using a calendar and I want to change it to jalali calendar, this my functions:

$('#calendar').fullCalendar({
            header: hdr,
            editable: true,
            droppable: true, // this allows things to be dropped onto the calendar !!!
            drop: function drop(date) {
                // this function is called when something is dropped

                // retrieve the dropped element's stored Event Object
                var originalEventObject = $(this).data('eventObject');

                // we need to copy it, so that multiple events don't have a reference to the same object
                var copiedEventObject = $.extend({}, originalEventObject);

                // assign it the date that was reported
                copiedEventObject.start = date;

                // render the event on the calendar
                // the last `true` argument determines if the event "sticks" (/)
                $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

                // is the "remove after drop" checkbox checked?
                if ($('#drop-remove').is(':checked')) {
                    // if so, remove the element from the "Draggable Events" list
                    $(this).remove();
                }
            },
            windowResize: function windowResize(event, ui) {
                $('#calendar').fullCalendar('render');
            }
        });

I have a function like this:

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var hdr = {};

and there is another function to convert it:

function gregorian_to_jalali($g_y,$g_m,$g_d,$mod=''){
    $g_y=tr_num($g_y); $g_m=tr_num($g_m); $g_d=tr_num($g_d);
 $d_4=$g_y%4;
 $g_a=array(0,0,31,59,90,120,151,181,212,243,273,304,334);
 $doy_g=$g_a[(int)$g_m]+$g_d;
 if($d_4==0 and $g_m>2)$doy_g++;
 $d_33=(int)((($g_y-16)%132)*.0305);
 $a=($d_33==3 or $d_33<($d_4-1) or $d_4==0)?286:287;
 $b=(($d_33==1 or $d_33==2) and ($d_33==$d_4 or $d_4==1))?78:(($d_33==3 and $d_4==0)?80:79);
 if((int)(($g_y-10)/63)==30){$a--;$b++;}
 if($doy_g>$b){
  $jy=$g_y-621; $doy_j=$doy_g-$b;
 }else{
  $jy=$g_y-622; $doy_j=$doy_g+$a;
 }
 if($doy_j<187){
  $jm=(int)(($doy_j-1)/31); $jd=$doy_j-(31*$jm++);
 }else{
  $jm=(int)(($doy_j-187)/30); $jd=$doy_j-186-($jm*30); $jm+=7;
 }
 return($mod=='')?array($jy,$jm,$jd):$jy.$mod.$jm.$mod.$jd;
}

I'm just confused how to use this functions to convert that hdr date to jalali date? I tried this but didn't work:

1

var date = new jdate();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

var hdr = {};

2 also this:

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var hdr = gregorian_to_jalali(y,m,d);

//var hdr = {};

What do I do wrong?

and what this line do: var hdr = {}; ?

let me explain whole of my work, I using a calendar and I want to change it to jalali calendar, this my functions:

$('#calendar').fullCalendar({
            header: hdr,
            editable: true,
            droppable: true, // this allows things to be dropped onto the calendar !!!
            drop: function drop(date) {
                // this function is called when something is dropped

                // retrieve the dropped element's stored Event Object
                var originalEventObject = $(this).data('eventObject');

                // we need to copy it, so that multiple events don't have a reference to the same object
                var copiedEventObject = $.extend({}, originalEventObject);

                // assign it the date that was reported
                copiedEventObject.start = date;

                // render the event on the calendar
                // the last `true` argument determines if the event "sticks" (http://arshaw./fullcalendar/docs/event_rendering/renderEvent/)
                $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

                // is the "remove after drop" checkbox checked?
                if ($('#drop-remove').is(':checked')) {
                    // if so, remove the element from the "Draggable Events" list
                    $(this).remove();
                }
            },
            windowResize: function windowResize(event, ui) {
                $('#calendar').fullCalendar('render');
            }
        });
Share asked Dec 6, 2017 at 8:32 MSH DeveloperMSH Developer 751 silver badge15 bronze badges 4
  • The information that you have supplied is inplete. Try #2 should have worked and the result of the conversion is stored in variable hdr. Suggestion: there is free code out there that might be of some help. A quick google search returned this site (farhadi.ir/projects/jalalijscalendar). – user2417483 Commented Dec 6, 2017 at 8:49
  • do you know what is this {} doing here: var hdr = {}; ? – MSH Developer Commented Dec 6, 2017 at 8:51
  • 1 It is creating an empty object - one that will be filled with the return value of gregorian_to_jalali(y,m,d) – user2417483 Commented Dec 6, 2017 at 8:53
  • You should properly format the code, replace all instances of "or" with "||", all instances of "and" with "&&", fix syntax errors like $g_a[(int)$g_m] and declare all variables with var. Lastly, provide the "tr_num" function. Until then, no help can be provided. Until you fix the gregorian_to_jalali function, the rest is pointless. – RobG Commented Dec 6, 2017 at 9:36
Add a ment  | 

4 Answers 4

Reset to default 7

March 2022 Solution

A simple method to convert Gregorian Dates to Persian (Jalali) Dates is to use Javascript's Intl.DateTimeFormat().
Here is how you can use it to convert today's date to Persian (Jalali) date:

const date = new Date(); // today's date

console.log("In Enlish               : ", new Intl.DateTimeFormat('en-u-ca-persian', { dateStyle: 'full' }).format(date));
console.log("In Persian              : ", new Intl.DateTimeFormat('fa-u-ca-persian', { dateStyle: 'full' }).format(date));
console.log("In Persian Latin Numbers: ", new Intl.DateTimeFormat('fa-u-ca-persian-nu-latn', { dateStyle: 'full' }).format(date));
console.log("In Arabic               : ", new Intl.DateTimeFormat('ar-u-ca-persian', { dateStyle: 'full' }).format(date));

simple Way to Convert Gregorian date to Jalali date

function gregorian_to_jalali(gy, gm, gd){
    g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
    if (gy > 1600) {
            jy = 979;
            gy -= 1600;
    }
    else {
            jy = 0;
            gy -= 621;
    }
    gy2 = (gm > 2) ? (gy + 1) : gy;
    days = (365 * gy) + (parseInt((gy2 + 3) / 4)) - (parseInt((gy2 + 99) / 100)) + (parseInt((gy2 + 399) / 400)) - 80 + gd + g_d_m[gm - 1];
    jy += 33 * (parseInt(days / 12053));
    days %= 12053;
    jy += 4 * (parseInt(days / 1461));
    days %= 1461;
    if (days > 365) {
            jy += parseInt((days - 1) / 365);
            days = (days - 1) % 365;
    }
    jm = (days < 186) ? 1 + parseInt(days / 31) : 7 + parseInt((days - 186) / 30);
    jd = 1 + ((days < 186) ? (days % 31) : ((days - 186) % 30));

    var resultY = jy.toString();
    var resultM = jm < 10 ? "0" + jm.toString() : jm.toString();
    var resultD = jd < 10 ? "0" + jd.toString() : jd.toString();
    return [resultY, resultM, resultD];
}

Use toLocaleDateString method. Here is the code:

let currentDate = new Date().toLocaleDateString('fa-IR-u-nu-latn',{year:'numeric',month:'2-digit',day:'2-digit'});

// the output is 1402/06/04.

You may input an specific date inside date as follows:

const event = new Date(Date.UTC(2023, 07, 20, 3, 0, 0)).toLocaleDateString('fa-IR-u-nu-latn',{year:'numeric',month:'2-digit',day:'2-digit'});

I used jalali-moment to change calendar system in a calendar.

发布评论

评论列表(0)

  1. 暂无评论