i have function to convert gregorian dates to jalali date,its work for one tag,but i have unspecified number of this tags in one page, and it must convert all of theme.
You have a few issues with your code:
There is no such thing as a date element. You should use a div instead.
An id (identifier) must be unique to its element, it cannot be repeated on other elements. Instead, you should use a class. At the moment you have a date class on all your elements expect the first. Thus, if you add the date class to your first element. Once you have done that you can use document.getElementsByClassName() to get all the elements on your page which have the class date.
document.getElementsByClassName() will return a HTMLCollection, and thus you can turn it into an array using the spread syntax (...). Turning your collection into an array will allow you to loop over it using .forEach. Using .forEach you can access every element with the class date, which you can then use to pull it's data-* attributes from.
See working example below:
function gregorian_to_jalali(gy, gm, gd) {
g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
if (gy > 1600) {
jy = 979;
gy -= 1600;
} else {
jy = 0;
gy -= 621;
}
gy2 = (gm > 2) ? (gy + 1) : gy;
days = (365 * gy) + (parseInt((gy2 + 3) / 4)) - (parseInt((gy2 + 99) / 100)) + (parseInt((gy2 + 399) / 400)) - 80 + gd + g_d_m[gm - 1];
jy += 33 * (parseInt(days / 12053));
days %= 12053;
jy += 4 * (parseInt(days / 1461));
days %= 1461;
if (days > 365) {
jy += parseInt((days - 1) / 365);
days = (days - 1) % 365;
}
jm = (days < 186) ? 1 + parseInt(days / 31) : 7 + parseInt((days - 186) / 30);
jd = 1 + ((days < 186) ? (days % 31) : ((days - 186) % 30));
return [jy, jm, jd];
}
const dateElements = document.getElementsByClassName("date"); // get all elements with the class "date"
[...dateElements].forEach(function(dateElement) { // loop through each element, where dateElement is the current node
const year = Number(dateElement.dataset.year);
const month = Number(dateElement.dataset.month);
const day = Number(dateElement.dataset.day);
dateElement.textContent = gregorian_to_jalali(year, month, day).join('/');
})