skip to Main Content

How to add time display with seconds to this working date code. Time should be shown UTC +1. And is it possible for it to automatically switch to winter and summer time?

Unfortunately, I don’t understand script programming.
Here is the working code that I have

var d = new Date();
var day=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var month=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
document.write(day[d.getDay()]+" " +d.getDate()+ " " + month[d.getMonth()]
                        + " " + d.getFullYear() + " Year.");

2

Answers


  1. You can use getSeconds & getMinutes methods

    const d = new Date();
    const day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    const month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    
    console.log(
      day[d.getDay()] + " " +
      d.getDate() + " " +
      month[d.getMonth()] + " " +
      d.getFullYear() + " Year " +
      d.getHours() + ' hours ' +
      d.getMinutes() + ' minutes ' +
      d.getSeconds() + ' seconds');
    Login or Signup to reply.
  2. To add the time display with seconds to your existing date code and show it in UTC+1 (with automatic switching between summer and winter time), you can update your script as follows:

    var d = new Date();
    var day = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var month = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    
    // Get local time and convert it to UTC+1
    var localTime = d.getTime();
    var localOffset = d.getTimezoneOffset() * 60000; // Timezone offset in milliseconds
    var utc = localTime + localOffset;
    var offset = 1; // UTC +1
    
    // Adjust for Daylight Saving Time (DST)
    var dst = d.getTimezoneOffset() < Math.max(new Date(d.getFullYear(), 0, 1).getTimezoneOffset(), new Date(d.getFullYear(), 6, 1).getTimezoneOffset()) ? 1 : 0;
    var adjustedTime = new Date(utc + (3600000 * (offset + dst)));
    
    // Format hours, minutes, and seconds with leading zeros
    var hours = String(adjustedTime.getHours()).padStart(2, '0');
    var minutes = String(adjustedTime.getMinutes()).padStart(2, '0');
    var seconds = String(adjustedTime.getSeconds()).padStart(2, '0');
    
    // Display the date and time
    document.write(day[d.getDay()] + " " + d.getDate() + " " + month[d.getMonth()] + " " + d.getFullYear() + " Year, " + hours + ":" + minutes + ":" + seconds + " UTC+1");
    

    Explanation:

    • UTC+1 Time: The code calculates UTC time and then adjusts it to UTC+1. The getTimezoneOffset() method is used to account for the difference between UTC and local time.
    • Daylight Saving Time: The code checks whether the current date falls under Daylight Saving Time and adjusts the time accordingly by adding an extra hour.

    This script should display the correct date and time in UTC+1, accounting for both summer and winter time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search