2017-10-13 55 views
1

您好我正在Angularjs/JavaScript中開發Web應用程序。我有一個壓光機選擇器,我從前端捕捉日期。在數據庫中保存選定的日期時,我想以Hijri格式保存。我跟着Converting Gregorian date to Hijri date,它在writeIslamicDate中傳遞1時正常工作。 例如:如何將javascript日期轉換爲hijri格式?

var today=new Date(); 
writeIslamicDate(today); 

如果我通過今天的日期,那麼它給了我undefined, NaN undefined NaN AH,所以我知道我需要通過輸入writeIslamicDate之前轉換爲任何格式?有人可以幫助我嗎?任何幫助,將不勝感激。謝謝。

回答

2

可以使用矩hijri.js它強大的庫來轉換日期

Git Repo

moment('2014-11-28 16:40:00', 'YYYY-M-D HH:mm:ss').endOf('iMonth').format('iYYYY/iM/iD HH:mm:ss'); // 1436/2/30 23:59:59 

JSFIDDEL Exemple

+0

感謝。那麼是否應該下載moment.js對不對?我試着用document.write(moment('2014-11-28 16:40:00','YYYY-MD HH:mm:ss')。endOf('iMonth')。format('iYYYY/iM/iH HH :MM:SS'));並沒有得到什麼 –

+0

是的應該,你可以使用鮑爾https://github.com/xsoh/moment-hijri –

+0

什麼可能是2014-11-28 16:40:00回覆日期?我得到了i2014/i11/i28 16:40:00? –

3

所有你需要的是一個輕微的調整,你鏈接的功能。要允許一個日期的可選調整的輸入,你需要允許傳遞一個日期,以便進行以下修改:

function writeIslamicDate(adjustment) { 
    ... 
    var iDate = kuwaiticalendar(adjustment); 

function writeIslamicDate(date, adjustment) { 
    ... 
    var iDate = kuwaiticalendar(date, adjustment); 

function kuwaiticalendar(adjust) { 
    var today = new Date(); 

到:

function kuwaiticalendar(date, adjust) { 
    var today = date || new Date(); 

然後調用它無論是否約會。下面是完整的修改代碼(稍微清理一下)。您還可以嘗試帶有選項的toLocaleString,該選項不僅可以轉換爲伊斯蘭日曆,還可以使用主機的默認語言。

// Returns mathematic mod, not javascript mod 
 
// e.g. gmod(-3, 2) returns 1, whereas -3%2 returns -1 
 
function gmod(n, m){ 
 
    return ((n % m) + m) % m; 
 
} 
 

 
/* @param {Date} date - optional, default is today 
 
** @param {number} adjust - optiona, days to adjust date by 
 
*/ 
 
function kuwaiticalendar(date, adjust) { 
 
    var today = date? new Date(+date) : new Date(); 
 
    if (adjust) { 
 
    today.setDate(today.getDate() + +adjust); 
 
    } 
 

 
    var day = today.getDate(); 
 
    var month = today.getMonth(); 
 
    var year = today.getFullYear(); 
 
    var m = month + 1; 
 
    var y = year; 
 

 
    if (m < 3) { 
 
     y -= 1; 
 
     m += 12; 
 
    } 
 

 
    var a = Math.floor(y/100); 
 
    var 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; 
 
    } 
 
    } 
 

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

 
    var bb = jd+b+1524; 
 
    var cc = Math.floor((bb - 122.1)/365.25); 
 
    var dd = Math.floor(365.25 * cc); 
 
    var 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; 
 
    var wd = gmod(jd + 1, 7) + 1; 
 

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

 
    var shift1 = 8.01/60.; 
 

 
    var z = jd-epochastro; 
 
    var cyc = Math.floor(z/10631.); 
 
    z = z - 10631 * cyc; 
 
    var j = Math.floor((z - shift1)/iyear); 
 
    var iy = 30 * cyc + j; 
 
    z = z - Math.floor(j * iyear + shift1); 
 
    var im = Math.floor((z + 28.5001)/29.5); 
 

 
    if (im == 13) im = 12; 
 
    var id = z-Math.floor(29.5001*im-29); 
 

 
    return [ 
 
    day,  //calculated day (CE) 
 
    month - 1, //calculated month (CE) 
 
    year,  //calculated year (CE) 
 
    jd - 1, //julian day number 
 
    wd - 1, //weekday number 
 
    id,  //islamic date 
 
    im - 1, //islamic month 
 
    iy   //islamic year 
 
    ]; 
 
} 
 

 
function writeIslamicDate(date, adjustment) { 
 
    var wdNames = ["Ahad", "Ithnin", "Thulatha", "Arbaa", "Khams", "Jumuah", "Sabt"]; 
 
    var iMonthNames = ["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(date, adjustment); 
 
    var outputIslamicDate = wdNames[iDate[4]] + ", " + iDate[5] + " " + 
 
          iMonthNames[iDate[6]] + " " + iDate[7] + " AH"; 
 
    return outputIslamicDate; 
 
} 
 

 
// No date or adjust 
 
console.log(writeIslamicDate()); 
 
// No date, adjust to tomorrow 
 
console.log(writeIslamicDate(undefined, 1)); 
 
// Date for 1 Jan 2017 
 
console.log(writeIslamicDate(new Date(2017,0,1))); 
 
// Date for 1 Jan 2017, adjust by -1 day 
 
console.log(writeIslamicDate(new Date(2017,0,1), -1));

使用的toLocaleString你會做類似於下面的東西。

// date is optional, defaults to today 
 
function writeHijri(date, lang) { 
 
    var date = date || new Date(); 
 
    lang = lang || 'en'; 
 
    var options = { 
 
    year: 'numeric', month: 'long', day: 'numeric' 
 
    }; 
 
    return date.toLocaleString(lang + '-u-ca-islamic', options); 
 
} 
 

 
// Today in English 
 
console.log(writeHijri()); 
 

 
// 1 Jan 2017 converted to Hijri (3 Rab. II 1438) in Arabic 
 
console.log(writeHijri(new Date(2017,0,1), 'ar'));

相關問題