将公历日期转换为回历日期

发布于 2024-10-20 02:22:00 字数 40 浏览 7 评论 0原文

如何使用 JavaScript 将公历日期转换为伊斯兰教回历日期?

How do you convert Gregorian dates to Islamic Hijri dates using JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诠释孤独 2024-10-27 02:22:01

最安全的是使用内置的 javascript Intl.DateTimeFormat() 构造函数

以下是伊斯兰回历今天日期的 4 种输出格式的示例。

还有在不同格式下分别提取年份和月份的示例。

let myFormat = 'en-u-ca-islamic-umalqura-nu-latn'; // use islamic-umalqura calendar (most modern)
let myDate   = new Date(Date.now());  // today's date
let output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'full'}).format(myDate);
console.log("Full format   : "+output);

output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'long'}).format(myDate);
console.log("Long format   : "+output);

output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'medium'}).format(myDate);
console.log("Medium format : "+output);

output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'short'}).format(myDate);
console.log("Short format (m/d/yyyy): "+output);

console.log("=".repeat(50));

let yearFull = new Intl.DateTimeFormat(myFormat,{year:'numeric'}).format(myDate);
console.log("The full year     : "+yearFull);
console.log("The year number   : " +(+yearFull.split(" ")[0]));

let monthLong = new Intl.DateTimeFormat(myFormat,{month:'long'}).format(myDate);
let monthShort = new Intl.DateTimeFormat(myFormat,{month:'short'}).format(myDate);
let monthNum = new Intl.DateTimeFormat(myFormat,{month:'numeric'}).format(myDate);
let month2 = new Intl.DateTimeFormat(myFormat,{month:'2-digit'}).format(myDate);
console.log("The long month    : "+monthLong);
console.log("The short month   : "+monthShort);
console.log("The month number  : "+monthNum);
console.log("The month 2 digits: "+month2);

The safest is to use the built-in javascript Intl.DateTimeFormat() constructor.

Here is an example of the 4 output formats in the Islamic Hijri Calendar of today's date.

Also examples of extracting the year and months separately under different formats.

let myFormat = 'en-u-ca-islamic-umalqura-nu-latn'; // use islamic-umalqura calendar (most modern)
let myDate   = new Date(Date.now());  // today's date
let output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'full'}).format(myDate);
console.log("Full format   : "+output);

output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'long'}).format(myDate);
console.log("Long format   : "+output);

output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'medium'}).format(myDate);
console.log("Medium format : "+output);

output   = new Intl.DateTimeFormat(myFormat,{dateStyle:'short'}).format(myDate);
console.log("Short format (m/d/yyyy): "+output);

console.log("=".repeat(50));

let yearFull = new Intl.DateTimeFormat(myFormat,{year:'numeric'}).format(myDate);
console.log("The full year     : "+yearFull);
console.log("The year number   : " +(+yearFull.split(" ")[0]));

let monthLong = new Intl.DateTimeFormat(myFormat,{month:'long'}).format(myDate);
let monthShort = new Intl.DateTimeFormat(myFormat,{month:'short'}).format(myDate);
let monthNum = new Intl.DateTimeFormat(myFormat,{month:'numeric'}).format(myDate);
let month2 = new Intl.DateTimeFormat(myFormat,{month:'2-digit'}).format(myDate);
console.log("The long month    : "+monthLong);
console.log("The short month   : "+monthShort);
console.log("The month number  : "+monthNum);
console.log("The month 2 digits: "+month2);

我做我的改变 2024-10-27 02:22:01

在 Javascript 中要转换日期,您可以使用 Intl (阅读更多)如下:

a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)

In Javascript to convert the date, you could use Intl (read more) as following:

a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)
初心未许 2024-10-27 02:22:00
function gmod(n,m){
return ((n%m)+m)%m;
}

function kuwaiticalendar(adjust){
var today = new Date();
if(adjust) {
    adjustmili = 1000*60*60*24*adjust; 
    todaymili = today.getTime()+adjustmili;
    today = new Date(todaymili);
}
day = today.getDate();
month = today.getMonth();
year = today.getFullYear();
m = month+1;
y = year;
if(m<3) {
    y -= 1;
    m += 12;
}

a = Math.floor(y/100.);
b = 2-a+Math.floor(a/4.);
if(y<1583) b = 0;
if(y==1582) {
    if(m>10)  b = -10;
    if(m==10) {
        b = 0;
        if(day>4) b = -10;
    }
}

jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;

b = 0;
if(jd>2299160){
    a = Math.floor((jd-1867216.25)/36524.25);
    b = 1+a-Math.floor(a/4.);
}
bb = jd+b+1524;
cc = Math.floor((bb-122.1)/365.25);
dd = Math.floor(365.25*cc);
ee = Math.floor((bb-dd)/30.6001);
day =(bb-dd)-Math.floor(30.6001*ee);
month = ee-1;
if(ee>13) {
    cc += 1;
    month = ee-13;
}
year = cc-4716;


wd = gmod(jd+1,7)+1;

iyear = 10631./30.;
epochastro = 1948084;
epochcivil = 1948085;

shift1 = 8.01/60.;

z = jd-epochastro;
cyc = Math.floor(z/10631.);
z = z-10631*cyc;
j = Math.floor((z-shift1)/iyear);
iy = 30*cyc+j;
z = z-Math.floor(j*iyear+shift1);
im = Math.floor((z+28.5001)/29.5);
if(im==13) im = 12;
id = z-Math.floor(29.5001*im-29);

var myRes = new Array(8);

myRes[0] = day; //calculated day (CE)
myRes[1] = month-1; //calculated month (CE)
myRes[2] = year; //calculated year (CE)
myRes[3] = jd-1; //julian day number
myRes[4] = wd-1; //weekday number
myRes[5] = id; //islamic date
myRes[6] = im-1; //islamic month
myRes[7] = iy; //islamic year

return myRes;
}
function writeIslamicDate(adjustment) {
var wdNames = new Array("Ahad","Ithnin","Thulatha","Arbaa","Khams","Jumuah","Sabt");
var iMonthNames = new Array("Muharram","Safar","Rabi'ul Awwal","Rabi'ul Akhir",
"Jumadal Ula","Jumadal Akhira","Rajab","Sha'ban",
"Ramadan","Shawwal","Dhul Qa'ada","Dhul Hijja");
var iDate = kuwaiticalendar(adjustment);
var outputIslamicDate = wdNames[iDate[4]] + ", " 
+ iDate[5] + " " + iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}

这会将当前计算机日期转换为回历。通过一点修改,您可以实现此代码段将任何日期更改为伊斯兰日期

document.write(writeIslamicDate(1));

取自 本站

function gmod(n,m){
return ((n%m)+m)%m;
}

function kuwaiticalendar(adjust){
var today = new Date();
if(adjust) {
    adjustmili = 1000*60*60*24*adjust; 
    todaymili = today.getTime()+adjustmili;
    today = new Date(todaymili);
}
day = today.getDate();
month = today.getMonth();
year = today.getFullYear();
m = month+1;
y = year;
if(m<3) {
    y -= 1;
    m += 12;
}

a = Math.floor(y/100.);
b = 2-a+Math.floor(a/4.);
if(y<1583) b = 0;
if(y==1582) {
    if(m>10)  b = -10;
    if(m==10) {
        b = 0;
        if(day>4) b = -10;
    }
}

jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;

b = 0;
if(jd>2299160){
    a = Math.floor((jd-1867216.25)/36524.25);
    b = 1+a-Math.floor(a/4.);
}
bb = jd+b+1524;
cc = Math.floor((bb-122.1)/365.25);
dd = Math.floor(365.25*cc);
ee = Math.floor((bb-dd)/30.6001);
day =(bb-dd)-Math.floor(30.6001*ee);
month = ee-1;
if(ee>13) {
    cc += 1;
    month = ee-13;
}
year = cc-4716;


wd = gmod(jd+1,7)+1;

iyear = 10631./30.;
epochastro = 1948084;
epochcivil = 1948085;

shift1 = 8.01/60.;

z = jd-epochastro;
cyc = Math.floor(z/10631.);
z = z-10631*cyc;
j = Math.floor((z-shift1)/iyear);
iy = 30*cyc+j;
z = z-Math.floor(j*iyear+shift1);
im = Math.floor((z+28.5001)/29.5);
if(im==13) im = 12;
id = z-Math.floor(29.5001*im-29);

var myRes = new Array(8);

myRes[0] = day; //calculated day (CE)
myRes[1] = month-1; //calculated month (CE)
myRes[2] = year; //calculated year (CE)
myRes[3] = jd-1; //julian day number
myRes[4] = wd-1; //weekday number
myRes[5] = id; //islamic date
myRes[6] = im-1; //islamic month
myRes[7] = iy; //islamic year

return myRes;
}
function writeIslamicDate(adjustment) {
var wdNames = new Array("Ahad","Ithnin","Thulatha","Arbaa","Khams","Jumuah","Sabt");
var iMonthNames = new Array("Muharram","Safar","Rabi'ul Awwal","Rabi'ul Akhir",
"Jumadal Ula","Jumadal Akhira","Rajab","Sha'ban",
"Ramadan","Shawwal","Dhul Qa'ada","Dhul Hijja");
var iDate = kuwaiticalendar(adjustment);
var outputIslamicDate = wdNames[iDate[4]] + ", " 
+ iDate[5] + " " + iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}

This converts current computer date to hijri. And with a little modification you can achieve that this snippet change any date to islamic

document.write(writeIslamicDate(1));

Taken from This site

奈何桥上唱咆哮 2024-10-27 02:22:00

查看我的库 hijrah-date 这是 Hijrah 中的 Javascript 日期日历系统。

它还支持回历到公历以及公历到回历的转换。除了日期格式之外。

Checkout my library hijrah-date which is a Javascript date in the Hijrah calendar system.

It also supports Hijrah to Gregorian and Gregorian to Hijrah conversion. In addition to date formatting.

暖风昔人 2024-10-27 02:22:00

如果您只需要从公历日期(Miladi 日期)转换而来的回历日期年份,您可以简单地在 javascript 中编写此方程式:

var GregorianYear = (new Date()).getFullYear();
var HijriYear = Math.round((GregorianYear - 622) * (33 / 32));

您可以在母版页的页脚中使用这个简单的方程式,例如
??????????????????????????????????????????????????????????????????????????????

<script type="text/javascript">document.write((new Date()).getFullYear())</script> م - <script type="text/javascript">var y = (new Date()).getFullYear();var h = Math.round((y - 622) * (33 / 32));document.write(h)</script> هـ


كа فقق فففف ـ ... © 2014 هـ

你也可以使用嵌入在 asp 页面中的 C# 为:

<%= DateTime.Now.Year %> - <%=  Math.Round((DateTime.Now.Year - 622) * 1.03125) %>

will return : 2014 - 1436

最后,如果你需要转换为 UmAlQura 日期,只需尝试这行代码:

let _date = new Date('7/10/2019').toLocaleDateString('ar-SA').format('DD/MM/YYYY');

console.log(_date);

will return : ٧‏/١١‏/١٤٤٠ هـ

更新 (2021-10-11) : 如果您需要更精确的方程,您必须通过以下公式知道太阳年的当前日期:

var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);

然后您可以通过以下公式获得回历年:

HijriYear = ((GregorianYear-621.5643)*365.24225 + dayOfYear) / 354.36707

其中:

365.24225是太阳年的天数。

354.36707 是农历年的天数。

621.5643 是回历的精确公历日期(回历日期的开始日期)

HijriYear = ((2021-621.5643)*365.24225 + 284) / 354.36707 = 1,443.18444

所以当前回历使用 Math.floor(HijriYear) 函数将年份设置为 1443

您还可以使用分数:

var hijriDayOfYear  = (HijriYear - Math.floor(HijriYear)) * 354.36707

0.18444乘以354.36707来获取回历中的天数:

0.18444 * 354.36707 = 65.3:数字当前回历年份中的天数

Math.ceil(hijriDayOfYear / 29.530589) = 3 当前回历月份的数量

Math.floor(hijriDayOfYear % 29.530589) = 6 ± 1 数量 最后总结一下之前

的所有内容,只需使用以下 JS 代码:

var now = new Date()
var dayOfYear = Math.floor((new Date() - new Date(now.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
var hijriDate = ((now.getFullYear()-621.5643)*365.24225 + dayOfYear) / 354.36707
var hijriYear = Math.floor(hijriDate)
var hijriMonth = Math.ceil((hijriDate - Math.floor(hijriDate)) * 354.36707 / 29.530589)
var hijriDay = Math.floor((hijriDate - Math.floor(hijriDate)) * 354.36707 % 29.530589)
    
console.log(`${hijriYear}/${hijriMonth}/${hijriDay}`)

控制台输出示例:1443/3/6

将日期时间转换为回历日期的通用 JavaScript 函数:

function GetHijriDate(dateTime) {    
    var dayOfYear = Math.floor((dateTime - new Date(dateTime.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
    var hijriDate = ((dateTime.getFullYear() - 621.5643) * 365.24225 + dayOfYear) / 354.36707
    var hijriYear = Math.floor(hijriDate)
    var hijriMonth = Math.ceil((hijriDate - Math.floor(hijriDate)) * 354.36707 / 29.530589)
    var hijriDay = Math.floor((hijriDate - Math.floor(hijriDate)) * 354.36707 % 29.530589)
    return [hijriYear, hijriMonth , hijriDay]
}

在 C# 中:

    /// <summary>
    /// Gets the hijri date.
    /// </summary>
    /// <param name="date">The date.</param>
    /// <returns></returns>
    public static int[] GetHijriDate(DateTime date)
    {
        var yearOfHijra = 621.5643f;
        var daysInSolarYear = 365.24225f;
        var daysInLunarYear = 354.36707f;
        var daysInLunarMonth = 29.53058f;

        var hijriDate = ((date.Year - yearOfHijra) * daysInSolarYear  + date.DayOfYear) / daysInLunarYear;
        var hijriYear = (int)Math.Floor(hijriDate);
        var hijriMonth = (int)Math.Ceiling((hijriDate - Math.Floor(hijriDate)) * daysInLunarYear / daysInLunarMonth);
        var hijriDay = (int)Math.Floor((hijriDate - Math.Floor(hijriDate)) * daysInLunarYear % daysInLunarMonth);

        int[] hijriDateRes = new int[3];
        hijriDateRes[0] = hijriYear;
        hijriDateRes[1] = hijriMonth;
        hijriDateRes[2] = hijriDay;

        return hijriDateRes;
    }

或简单地:

console.log(new Date().toLocaleDateString('ar-SA'))

'٥‏/٣‏/ ١٤٤٣ هـ'

If you need only the year of Hijri date converted from Gregorian date (Miladi date) you can simply write this equation in javascript:

var GregorianYear = (new Date()).getFullYear();
var HijriYear = Math.round((GregorianYear - 622) * (33 / 32));

you can use this simple equation in the footer in master page like
كل الحقوق محفوطة لـ ... ©

<script type="text/javascript">document.write((new Date()).getFullYear())</script> م - <script type="text/javascript">var y = (new Date()).getFullYear();var h = Math.round((y - 622) * (33 / 32));document.write(h)</script> هـ

you will get:
كل الحقوق محفوطة لـ ... © 2014 م - 1435 هـ

you can also use C# embedded in asp page as:

<%= DateTime.Now.Year %> - <%=  Math.Round((DateTime.Now.Year - 622) * 1.03125) %>

will return : 2014 - 1436

Finally, if you need to convert to UmAlQura date, simply try this line of code:

let _date = new Date('7/10/2019').toLocaleDateString('ar-SA').format('DD/MM/YYYY');

console.log(_date);

will return : ٧‏/١١‏/١٤٤٠ هـ

Update (2021-10-11) : If you need a more precise equation you have to know the current day of solar year by this formula :

var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var dayOfYear = Math.floor(diff / oneDay);

Then you can get the Hijri year by this formula:

HijriYear = ((GregorianYear-621.5643)*365.24225 + dayOfYear) / 354.36707

Where :

365.24225 is the number of days in solar year.

354.36707 is the number of days in lunar year.

621.5643 is the exact Gregorian date of Hijra (start date of Hijri date)

HijriYear = ((2021-621.5643)*365.24225 + 284) / 354.36707 = 1,443.18444

So the current Hijri year is 1443 by using Math.floor(HijriYear) function.

Also you can use the fraction :

var hijriDayOfYear  = (HijriYear - Math.floor(HijriYear)) * 354.36707

0.18444 multiplied by 354.36707 to get the number of days in Hijri calendar :

0.18444 * 354.36707 = 65.3 : the number of day in this current Hijri year

Math.ceil(hijriDayOfYear / 29.530589) = 3 number of current Hijri month

Math.floor(hijriDayOfYear % 29.530589) = 6 ± 1 number of day in this Hijri month

Finally to summarize all of previous just use the following JS code:

var now = new Date()
var dayOfYear = Math.floor((new Date() - new Date(now.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
var hijriDate = ((now.getFullYear()-621.5643)*365.24225 + dayOfYear) / 354.36707
var hijriYear = Math.floor(hijriDate)
var hijriMonth = Math.ceil((hijriDate - Math.floor(hijriDate)) * 354.36707 / 29.530589)
var hijriDay = Math.floor((hijriDate - Math.floor(hijriDate)) * 354.36707 % 29.530589)
    
console.log(`${hijriYear}/${hijriMonth}/${hijriDay}`)

console output sample : 1443/3/6

General JavaScript Function to convert datetime to Hijri date:

function GetHijriDate(dateTime) {    
    var dayOfYear = Math.floor((dateTime - new Date(dateTime.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))
    var hijriDate = ((dateTime.getFullYear() - 621.5643) * 365.24225 + dayOfYear) / 354.36707
    var hijriYear = Math.floor(hijriDate)
    var hijriMonth = Math.ceil((hijriDate - Math.floor(hijriDate)) * 354.36707 / 29.530589)
    var hijriDay = Math.floor((hijriDate - Math.floor(hijriDate)) * 354.36707 % 29.530589)
    return [hijriYear, hijriMonth , hijriDay]
}

In C# :

    /// <summary>
    /// Gets the hijri date.
    /// </summary>
    /// <param name="date">The date.</param>
    /// <returns></returns>
    public static int[] GetHijriDate(DateTime date)
    {
        var yearOfHijra = 621.5643f;
        var daysInSolarYear = 365.24225f;
        var daysInLunarYear = 354.36707f;
        var daysInLunarMonth = 29.53058f;

        var hijriDate = ((date.Year - yearOfHijra) * daysInSolarYear  + date.DayOfYear) / daysInLunarYear;
        var hijriYear = (int)Math.Floor(hijriDate);
        var hijriMonth = (int)Math.Ceiling((hijriDate - Math.Floor(hijriDate)) * daysInLunarYear / daysInLunarMonth);
        var hijriDay = (int)Math.Floor((hijriDate - Math.Floor(hijriDate)) * daysInLunarYear % daysInLunarMonth);

        int[] hijriDateRes = new int[3];
        hijriDateRes[0] = hijriYear;
        hijriDateRes[1] = hijriMonth;
        hijriDateRes[2] = hijriDay;

        return hijriDateRes;
    }

Or Simply :

console.log(new Date().toLocaleDateString('ar-SA'))

'٥‏/٣‏/١٤٤٣ هـ'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文