-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-fa.js
More file actions
109 lines (101 loc) · 4.5 KB
/
script-fa.js
File metadata and controls
109 lines (101 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
document.addEventListener('DOMContentLoaded', function() {
const startDateInput = document.getElementById('start-date');
const endDateInput = document.getElementById('end-date');
const calcBtn = document.getElementById('calc-btn');
const resultDiv = document.getElementById('result');
// عملکرد دوم
const baseDateInput = document.getElementById('base-date');
const directionSelect = document.getElementById('direction');
const daysInput = document.getElementById('days');
const calcDateBtn = document.getElementById('calc-date-btn');
const resultDateDiv = document.getElementById('result-date');
// تاریخ شروع پیشفرض امروز است
const today = new Date();
function toDateString(date) {
return date.toISOString().split('T')[0];
}
startDateInput.value = toDateString(today);
baseDateInput.value = toDateString(today);
const WEEKDAYS = ['یکشنبه', 'دوشنبه', 'سهشنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه', 'شنبه'];
function getYMDdiff(start, end) {
let y = end.getFullYear() - start.getFullYear();
let m = end.getMonth() - start.getMonth();
let d = end.getDate() - start.getDate();
if (d < 0) {
m--;
let prevMonth = new Date(end.getFullYear(), end.getMonth(), 0);
d += prevMonth.getDate();
}
if (m < 0) {
y--;
m += 12;
}
return {y, m, d};
}
function showResult(diffMs, start, end) {
const absDiffMs = Math.abs(diffMs);
const diffDays = Math.floor(absDiffMs / (1000 * 60 * 60 * 24));
let text = '';
if (diffMs < 0) {
text = `تاریخ هدف ${diffDays} روز قبل از تاریخ شروع است.`;
if (diffDays > 30 || diffDays > 365) {
const {y, m, d} = getYMDdiff(end, start);
text += `<br>(تفاوت: ${y ? y + ' سال ' : ''}${m ? m + ' ماه ' : ''}${d ? d + ' روز ' : ''})`;
}
} else {
text = `تفاوت: ${diffDays} روز.`;
if (diffDays > 30 || diffDays > 365) {
const {y, m, d} = getYMDdiff(start, end);
text += `<br>(تفاوت: ${y ? y + ' سال ' : ''}${m ? m + ' ماه ' : ''}${d ? d + ' روز ' : ''})`;
}
}
resultDiv.innerHTML = text;
}
function validateAndCalc() {
const startDateStr = startDateInput.value;
const endDateStr = endDateInput.value;
if (!startDateStr || !endDateStr) {
resultDiv.textContent = 'لطفاً هر دو تاریخ را وارد کنید.';
return;
}
const start = new Date(startDateStr);
const end = new Date(endDateStr);
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
resultDiv.textContent = 'فرمت تاریخ نامعتبر است.';
return;
}
let diffMs = end - start;
showResult(diffMs, start, end);
}
calcBtn.addEventListener('click', validateAndCalc);
// عملکرد دوم: محاسبه تاریخ بعد/قبل از N روز
function calcDate() {
const baseDateStr = baseDateInput.value;
const daysVal = daysInput.value;
const direction = directionSelect.value;
if (!baseDateStr || daysVal === '' || isNaN(Number(daysVal))) {
resultDateDiv.textContent = 'لطفاً تاریخ و تعداد روز را وارد کنید.';
return;
}
const base = new Date(baseDateStr);
const daysNum = parseInt(daysVal, 10);
if (isNaN(base.getTime()) || isNaN(daysNum)) {
resultDateDiv.textContent = 'تاریخ یا تعداد روز نامعتبر است.';
return;
}
let resultDate;
if (direction === 'after') {
resultDate = new Date(base.getTime() + daysNum * 24 * 60 * 60 * 1000);
} else {
resultDate = new Date(base.getTime() - daysNum * 24 * 60 * 60 * 1000);
}
// فرمت yyyy-mm-dd
const yyyy = resultDate.getFullYear();
const mm = String(resultDate.getMonth() + 1).padStart(2, '0');
const dd = String(resultDate.getDate()).padStart(2, '0');
const weekday = WEEKDAYS[resultDate.getDay()];
const resultStr = `${yyyy}-${mm}-${dd}`;
resultDateDiv.textContent = `نتیجه: ${resultStr} (${weekday})`;
}
calcDateBtn.addEventListener('click', calcDate);
});