แปลง YYYY-MM เป็น DD/MM/YYYY
2025-01 → 15/01/2025
ตัวแปลงด่วน
→
15/01/2025
วิธีแปลง YYYY-MM เป็น DD/MM/YYYY
การแปลงจาก YYYY-MM เป็น DD/MM/YYYY ต้องจัดเรียงส่วนประกอบวันที่ใหม่:
จาก
YYYY-MM
2025-01
เป็น
DD/MM/YYYY
15/01/2025
1
ระบุส่วนประกอบ
ในรูปแบบ YYYY-MM: Year-month format following ISO 8601, ideal for file naming and sorting
2
จัดเรียงใหม่
จัดลำดับใหม่ให้ตรงกับรูปแบบ DD/MM/YYYY: Day-Month-Year format commonly used in Europe, Asia, and most of the world
3
ปรับตัวคั่น
เปลี่ยนตัวคั่นจาก "-" เป็น "/" หากจำเป็น
ตัวอย่างโค้ด
JavaScript
// แปลง YYYY-MM เป็น DD/MM/YYYY
function convertDate(dateStr) {
// แปลง YYYY-MM
const parts = dateStr.split('-');
const [year, month, day] = parts;
// จัดรูปแบบเป็น DD/MM/YYYY
return `${day}/${month}/${year}`;
}
console.log(convertDate('2025-01')); // 15/01/2025
Python
from datetime import datetime
# แปลง YYYY-MM เป็น DD/MM/YYYY
date_str = '2025-01'
date = datetime.strptime(date_str, '%Y-%m-%d')
result = date.strftime('%d/%m/%Y')
print(result) # 15/01/2025