Datumsformatierung nach Programmiersprache

Codebeispiele für die Datumsformatierung

Sprache wählen

Kurzreferenz: YYYY-MM-DD

So formatieren Sie ein Datum als YYYY-MM-DD (ISO 8601) in beliebten Sprachen:

💛 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-31

Allgemeine Format-Codes

Verschiedene Sprachen verwenden unterschiedliche Format-Codes. Hier ist ein Vergleich:

Bedeutung Python/PHP JavaScript SQL
Jahr (4-stellig) %Y getFullYear() %Y / YYYY
Jahr (2-stellig) %y manual %y / YY
Monat (01-12) %m getMonth()+1 %m / MM
Monatsname %B toLocaleDateString() %M / Month
Tag (01-31) %d getDate() %d / DD

Nach Format durchsuchen

Tipps zur Datumsformatierung

Best Practice: Verwenden Sie beim Speichern von Daten in Datenbanken oder APIs immer das ISO 8601-Format (YYYY-MM-DD). Es ist eindeutig, sortiert korrekt und wird universell verstanden.
Zeitzonen-Tipp: Beachten Sie Zeitzonenunterschiede. Die JavaScript-Methode toISOString() gibt UTC-Zeit zurück, während toLocaleDateString() die lokale Zeitzone verwendet.