Set time zone
let dateString = new Date().toLocaleString('de-DE', { timeZone: 'Europe/Amsterdam' });
Some date formats
// get hour and minute let now = new Date() let hours = now.getHours(); let minutes = now.getMinutes(); console.log(`Current time: ${hours}:${minutes}`);
// format 1-6-2024 let today = new Date(); let day = today.getDate(); let month = today.getMonth() + 1; // Months are zero-based let year = today.getFullYear(); console.log(`${day}-${month}-${year}`);
// format 01-06-2024 let today = new Date(); let day = String(today.getDate()).padStart(2, '0'); let month = String(today.getMonth() + 1).padStart(2, '0'); let year = today.getFullYear(); let formattedDate = `${day}-${month}-${year}`;
// format 12-20-23 ( hh-mm-ss ) let now = new Date(); let hours = String(now.getHours()).padStart(2, '0'); let minutes = String(now.getMinutes()).padStart(2, '0'); let seconds = String(now.getSeconds()).padStart(2, '0'); let dateString = `${hours}:${minutes}:${seconds}`;
Display elapsed time
function displayElapsedTime() {
const now = new Date();
const europeTimezoneOffset = 2; // GMT+2 for most of Europe
const nowEurope = new Date(now.getTime() + (europeTimezoneOffset * 60 * 60 * 1000));
const hours = String(nowEurope.getHours()).padStart(2, '0');
const minutes = String(nowEurope.getMinutes()).padStart(2, '0');
const seconds = String(nowEurope.getSeconds()).padStart(2, '0');
const formattedTime = `${hours}:${minutes}:${seconds}`;
document.getElementById('elapsed-time').textContent = formattedTime;
}
displayElapsedTime();
setInterval(displayElapsedTime, 30000); // Update every 30 seconds