change timstamp functional
This commit is contained in:
@@ -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