2

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)

Amir Naby
  • 41
  • 6
  • Timestamps are always in Gregorian calendar dates, unfortunately. – Mohsen Alyafei Mar 01 '22 at 16:40
  • Maybe you can convert a Jalali Date to a Gregorian Date and then convert that to a TimeStamp. I can post a short Javascript function to convert Jalali Dates to Gregorian Dates without the use of external libraries if that will answer your question or help you. – Mohsen Alyafei Mar 01 '22 at 16:41
  • Here is my method to convert Persian Dates to any other calendars without external libraries: https://stackoverflow.com/questions/71421825/how-to-convert-persian-jalali-dates-to-other-18-calendar-dates-in-javascript-w/71421832#71421832 – Mohsen Alyafei Mar 10 '22 at 09:49

1 Answers1

5

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));
//==========================================================
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42