【问题标题】:Convert gregorian date to jalali将公历日期转换为 jalali
【发布时间】:2017-12-06 08:32:01
【问题描述】:

我有这样的功能:

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

var hdr = {};

还有另一个函数可以转换它:

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;
}

我只是很困惑如何使用这个函数将 hdr 日期转换为 jalali 日期? 我试过了,但没有用:

1

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

var hdr = {};

2 还有这个:

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 = {};

我做错了什么?

以及这一行的作用: var hdr = {}; ?

让我解释一下我的整个工作,我使用日历,我想将其更改为 jalali 日历,这是我的功能:

$('#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.com/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');
            }
        });

【问题讨论】:

  • 您提供的信息不完整。尝试 #2 应该可以工作,并且转换的结果存储在变量 hdr 中。建议:那里有免费的代码可能会有所帮助。谷歌快速搜索返回了这个网站 (farhadi.ir/projects/jalalijscalendar)。
  • 你知道这个{}在这里做什么吗:var hdr = {}; ?
  • 它正在创建一个空对象 - 一个将填充 gregorian_to_jalali(y,m,d) 的返回值的对象
  • 你应该正确格式化代码,用“||”替换所有“or”实例,用“&&”替换所有“and”实例,修复$g_a[(int)$g_m]等语法错误并声明所有变量与 var。最后,提供“tr_num”函数。在此之前,无法提供任何帮助。在你修复 gregorian_to_jalali 函数之前,其余的都是没有意义的。

标签: javascript date jalali-calendar


【解决方案1】:

将公历日期转换为 Jalali 日期的简单方法

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];
}

【讨论】:

【解决方案2】:

我使用jalali-moment 更改日历中的日历系统。

【讨论】:

【解决方案3】:

2022 年 3 月解决方案

将公历日期转换为波斯 (Jalali) 日期的简单方法是使用 Javascript 的 Intl.DateTimeFormat()
您可以使用它来将今天的日期转换为波斯 (Jalali) 日期:

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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2021-04-29
    • 1970-01-01
    • 2015-11-22
    • 2013-07-21
    • 1970-01-01
    相关资源
    最近更新 更多