Formate Date sipas Gjuhës së Programimit
Shembuj kodi për formatimin e datës
Zgjidh Gjuhën
Referencë e Shpejtë: YYYY-MM-DD
Ja si të formatosh një datë si YYYY-MM-DD (ISO 8601) në gjuhët e njohura:
💛 JavaScript
const date = new Date();
// Method 1: toISOString (recommended)
const formatted = date.toISOString().split('T')[0];
console.log(formatted); // Output: 2025-12-31
// Method 2: Manual formatting
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
console.log(`${yyyy}-${mm}-${dd}`); // Output: 2025-12-31
🐍 Python
from datetime import datetime
date = datetime.now()
formatted = date.strftime('%Y-%m-%d')
print(formatted) # Output: 2025-12-31
🐘 PHP
<?php
echo date('Y-m-d'); // Output: 2025-12-31
🐬 MySQL
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') AS formatted_date;
-- Output: 2025-12-31Kode të Zakonshme Formati
Gjuhë të ndryshme përdorin kode të ndryshme formati.
| Kuptim | Python/PHP | JavaScript | SQL |
|---|---|---|---|
| Vit (4 shifra) | %Y | getFullYear() | %Y / YYYY |
| Vit (2 shifra) | %y | manual | %y / YY |
| Muaj (01-12) | %m | getMonth()+1 | %m / MM |
| Emri i muajit | %B | toLocaleDateString() | %M / Month |
| Ditë (01-31) | %d | getDate() | %d / DD |
Shfleto sipas Formatit
Këshilla për Formatimin e Datës
Praktika më e Mirë: Kur ruani data në baza të dhënash, gjithmonë përdorni formatin ISO 8601 (YYYY-MM-DD).
Këshillë për Zonën Kohore: Kini kujdes për ndryshimet e zonës kohore.