change timstamp functional
This commit is contained in:
@@ -11,6 +11,7 @@ import ArrowLeftD from "@/components/icons/ArrowLeftD";
|
||||
import ClockD from "@/components/icons/ClockD";
|
||||
import LikeD from "@/components/icons/LikeD";
|
||||
import PointD from "@/components/icons/PointD";
|
||||
import { convertTimestampToPersianDate, convertTimestampToPersianDateSimple } from "@/components/doctors/listDoctors/timestamp_to_persian_date";
|
||||
|
||||
moment.loadPersian({ dialect: "persian-modern" });
|
||||
|
||||
@@ -93,9 +94,7 @@ function ItemDoctor({ doctor, setDoctors }) {
|
||||
}`}
|
||||
>
|
||||
{Number(doctor?.active)
|
||||
? `اولین نوبت آزاد: ${moment(doctor?.free_turn * 1000).format(
|
||||
"dddd jD jMMMM [ساعت] HH:mm"
|
||||
)}`
|
||||
? `اولین نوبت آزاد: ${convertTimestampToPersianDateSimple(doctor?.free_turn)}`
|
||||
: "نوبت ندارد"}
|
||||
</p>
|
||||
</CustomLoading>
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
export function convertTimestampToPersianDate(timestamp) {
|
||||
// Convert timestamp to milliseconds (JavaScript uses milliseconds)
|
||||
const date = new Date(timestamp * 1000);
|
||||
|
||||
// Format with Persian calendar and Tehran timezone
|
||||
const options = {
|
||||
timeZone: "Asia/Tehran",
|
||||
calendar: "persian",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
weekday: "long",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hour12: true,
|
||||
};
|
||||
|
||||
const formatter = new Intl.DateTimeFormat("fa-IR", options);
|
||||
const parts = formatter.formatToParts(date);
|
||||
|
||||
// Extract parts
|
||||
const weekday = parts.find((part) => part.type === "weekday").value;
|
||||
const year = parts.find((part) => part.type === "year").value;
|
||||
const month = parts.find((part) => part.type === "month").value;
|
||||
const day = parts.find((part) => part.type === "day").value;
|
||||
const hour = parts.find((part) => part.type === "hour").value;
|
||||
const minute = parts.find((part) => part.type === "minute").value;
|
||||
const second = parts.find((part) => part.type === "second").value;
|
||||
const dayPeriod = parts.find((part) => part.type === "dayPeriod").value;
|
||||
|
||||
// Format the final string
|
||||
return `${weekday}, ${year}-${month}-${day} ${hour}:${minute}:${second} ${dayPeriod}`;
|
||||
}
|
||||
|
||||
// Alternative simpler approach using toLocaleString
|
||||
export function convertTimestampToPersianDateSimple(timestamp) {
|
||||
const date = new Date(timestamp * 1000);
|
||||
|
||||
const options = {
|
||||
timeZone: "Asia/Tehran",
|
||||
calendar: "persian",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
weekday: "long",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hour12: true,
|
||||
};
|
||||
|
||||
return date.toLocaleString("fa-IR", options);
|
||||
}
|
||||
|
||||
// Example usage
|
||||
const timestamp = 1759132200;
|
||||
|
||||
console.log("Method 1 (formatToParts):");
|
||||
console.log(convertTimestampToPersianDate(timestamp));
|
||||
|
||||
console.log("\nMethod 2 (toLocaleString):");
|
||||
console.log(convertTimestampToPersianDateSimple(timestamp));
|
||||
|
||||
// For more control over the format, you can create a custom formatter
|
||||
export function convertTimestampCustomFormat(timestamp) {
|
||||
const date = new Date(timestamp * 1000);
|
||||
|
||||
const options = {
|
||||
timeZone: "Asia/Tehran",
|
||||
calendar: "persian",
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
weekday: "long",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hour12: true,
|
||||
};
|
||||
|
||||
const formatter = new Intl.DateTimeFormat("fa-IR", options);
|
||||
const formatted = formatter.format(date);
|
||||
|
||||
// Add bullet point if needed
|
||||
return `* ${formatted}`;
|
||||
}
|
||||
|
||||
console.log("\nMethod 3 (with bullet point):");
|
||||
console.log(convertTimestampCustomFormat(timestamp));
|
||||
Reference in New Issue
Block a user