1
0
forked from Yara724/api

Fixed personal inquiry

This commit is contained in:
SepehrYahyaee
2026-05-09 16:17:09 +03:30
parent a52b7a0a72
commit eb648d8a87
2 changed files with 90 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
import * as jMoment from "jalali-moment";
/**
* Converts an ISO date string or Date to Jalali (Persian) date and time.
* @param input - ISO date string (e.g. 2026-02-08T13:51:20.747+00:00) or Date
@@ -12,6 +14,66 @@ export function toJalaliDateAndTime(
return [dateStr, timeStr];
}
/**
* Converts a Jalali (Persian) date in any of the common shapes used across
* this codebase to a Gregorian `YYYY-MM-DD` string.
*
* Accepted input shapes:
* - number `13770624` (packed `YYYYMMDD`)
* - string `"13770624"` / `"1377-06-24"` / `"1377/06/24"` / `"1377/6/24"`
* - already-Gregorian `YYYY-MM-DD` strings are returned untouched as long as
* they parse (we treat them as Gregorian only when the year is clearly
* Gregorian, i.e. >= 1900). Otherwise the year is treated as Jalali.
*
* Returns `null` when the input cannot be parsed into a valid Jalali date.
*/
export function jalaliToGregorianDate(
input: string | number | null | undefined,
): string | null {
if (input === null || input === undefined) return null;
const raw =
typeof input === "number" ? String(input) : String(input).trim();
if (!raw) return null;
let year = 0;
let month = 0;
let day = 0;
const separated = raw.match(/^(\d{4})[\-/](\d{1,2})[\-/](\d{1,2})$/);
if (separated) {
year = parseInt(separated[1], 10);
month = parseInt(separated[2], 10);
day = parseInt(separated[3], 10);
} else {
const digits = raw.replace(/\D/g, "");
if (digits.length === 8) {
year = parseInt(digits.slice(0, 4), 10);
month = parseInt(digits.slice(4, 6), 10);
day = parseInt(digits.slice(6, 8), 10);
} else {
return null;
}
}
if (!year || !month || !day) return null;
// If the year already looks Gregorian (>= 1900), assume the caller already
// sent a Gregorian date and just normalise the formatting.
if (year >= 1900) {
const mm = String(month).padStart(2, "0");
const dd = String(day).padStart(2, "0");
const m = jMoment(`${year}-${mm}-${dd}`, "YYYY-MM-DD");
return m.isValid() ? m.format("YYYY-MM-DD") : null;
}
const mm = String(month).padStart(2, "0");
const dd = String(day).padStart(2, "0");
const jm = jMoment(`${year}-${mm}-${dd}`, "jYYYY-jMM-jDD");
if (!jm.isValid()) return null;
return jm.format("YYYY-MM-DD");
}
/**
* Converts a Date to Jalali date string (e.g. 1404/11/24).
*/