0 Pluspunkte 0 Minuspunkte
Wie arbeitet man mit Datum und Zeit in Javascript?
von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Mit dem Date() Objekt.

var currentDate = new Date()
console.log(currentDate.getMonth());    // liefert 0 - 11
console.log(currentDate.getFullYear()); // YYYY (startet nicht bei 0)
console.log(currentDate.getDate());     // liefert 1 - 31
console.log(currentDate.getDay());      // Tag der Woche (startet am Sonntag mit 0)
console.log(currentDate.getHours());    // liefert 0 - 23
console.log(currentDate.getMinutes());  // liefert 0 - 59
console.log(currentDate.getTime());     // Millisekunden seit 1.1.1970

console.log("Current Date: " + currentDate.toDateString());
console.log("Current Time: " + currentDate.toTimeString());

var timestamp = Date.parse('2023-07-09 14:30:00');
console.log("Unix-Zeitstempel: " + timestamp);

var specificDate = new Date('2023-07-09T14:30:00');
console.log("Formatted Date: " + specificDate.toLocaleDateString());
console.log("Formatted Time: " + specificDate.toLocaleTimeString());

var specificDate = new Date('2023-07-09T14:30:00');
var specificDateUTC = specificDate.toUTCString();
console.log("Specific Date (UTC): " + specificDateUTC);

var specificDate = new Date('2023-07-09T14:30:00');
specificDate.setDate(specificDate.getDate() + 1); // Add 1 day
specificDate.setHours(specificDate.getHours() + 1); // Add 1 hour
console.log("New Date: " + specificDate.toLocaleDateString() + " " + specificDate.toLocaleTimeString());
von (776 Punkte)