Home > Net >  Convert Persian (Jalali) calendar to timestamp on form field entry
Convert Persian (Jalali) calendar to timestamp on form field entry

Time:03-04

I made a form with Confiforms in Confluence including a Persian calendar field. There is a Filter Control macro to filter the records. But this filter doesn't work fine with Persian dates. Because Persian dates are saved as timestamp but the Persian calendar field displays the date, not the timestamp, and when I want to filter records, I should write timestamp in the Persian calendar field to make filter work.

Form ScreenShot

The problem is I want that when I choose a date, it returned its timestamp to the field instead of date. (See the picture)

Is there any JavaScript/jQuery code to convert a Persian date entry of a field to timestamp?

Edit: The shamsi date format of field entry is d-m-yy (eg. 15-12-1400)

CodePudding user response:

The following function will convert a Persian (Jalali) Date to a Timestamp (UTC based).

You pass the Jalali's year, month, and day to the function and the output is the timestamp in the UTC zone.

function jalaliToUTCTimeStamp(year, month, day) {
const format=new Intl.DateTimeFormat('en-u-ca-persian',{dateStyle:'short',timeZone:"UTC"});
let g=new Date(Date.UTC(2000,month,day));
    g=new Date(g.setUTCDate(g.getUTCDate() 226867));
const gY=g.getUTCFullYear(g)-2000 year;
    g=new Date(((gY<0)?"-":" ") ("00000" Math.abs(gY)).slice(-6) "-" ("0" (g.getUTCMonth(g) 1)).slice(-2) "-" ("0" (g.getUTCDate(g))).slice(-2));
let [pM,pD,pY] = [...format.format(g).split("/")],i=0;
    g=new Date(g.setUTCDate(g.getUTCDate() ~~(year*365.25 month*30.44 day-(pY.split(" ")[0]*365.25 pM*30.44 pD*1))-2));
while (i<4){
[pM,pD,pY]=[...format.format(g).split("/")];
if(pD==day && pM==month && pY.split(" ")[0]==year)return  g;
g=new Date(g.setUTCDate(g.getUTCDate() 1)); i  ;
}
throw new Error('Invalid Persian Date!');
}
//==========================================================
// Test Units
//==========================================================
console.log(jalaliToUTCTimeStamp(1400,12,5));
console.log(jalaliToUTCTimeStamp(1400,12,6));
console.log(jalaliToUTCTimeStamp(1400,12,7));
console.log(jalaliToUTCTimeStamp(1400,12,8));
console.log(jalaliToUTCTimeStamp(1400,12,9));
console.log(jalaliToUTCTimeStamp(1400,12,10));
console.log(jalaliToUTCTimeStamp(1400,12,11));
//==========================================================

  • Related