您好我正在开发Angularjs / JavaScript中的Web应用程序。我有一个日历选择器,我从前端捕获日期。在数据库中保存所选日期时,我想以Hijri格式保存。我跟着Converting Gregorian date to Hijri date,当我在writeIslamicDate中传递1时它工作正常。 例如:
var today=new Date();
writeIslamicDate(today);
如果我今天通过日期,那么它会给我undefined, NaN undefined NaN AH
所以我可以知道在将输入传递给writeIslamicDate之前是否需要转换为任何格式?有人可以帮助我吗?任何帮助,将不胜感激。谢谢。
答案 0 :(得分:3)
您只需稍微调整您链接的功能即可。要允许输入日期作为可选调整,您需要允许传递日期,因此请进行以下更改:
double shuffle_signals(float array[VOL])
{
srand((unsigned int) time(NULL));
if(VOL>1)
{
int i,j,t;
for(i=0; i<VOL;i++)
{
j=i+rand()/((float)RAND_MAX/(VOL-i)+1.);
t=array[j];
array[j]=array[i];
array[i]=t;
if(array[i]==1){
signal2[i]=MIN;
signal1[i]=MAX;
}
else if(array[i]==0){
signal2[i]=MIN;
signal1[i]=MIN;
}
else if (array[i]==2){
signal1[i]=MAX;
signal2[i]=MAX;
}
tot_sig= signal1[i]+signal2[i];
}
// printf("tot_sign: %lf\n", tot_sig);
}
return tot_sig;
}
}
到
function writeIslamicDate(adjustment) {
...
var iDate = kuwaiticalendar(adjustment);
和
function writeIslamicDate(date, adjustment) {
...
var iDate = kuwaiticalendar(date, adjustment);
为:
function kuwaiticalendar(adjust) {
var today = new Date();
用日期与否来称呼它。下面是完整修改后的代码(清理了一下)。您还可以尝试toLocaleString选项,这些选项不仅可以转换为伊斯兰日历,还可以使用主机的默认语言。
function kuwaiticalendar(date, adjust) {
var today = date || new Date();
&#13;
使用 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));
&#13;
答案 1 :(得分:1)
你可以使用moment-hijri.js这是一个强大的库来转换日期
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