Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptHow to create a date with a set timezone without using a...

How to create a date with a set timezone without using a string representation?

For creating a date with a set timezone without using a string representation, Date Object method is used. More about this method can be learned from here. It is a built-in function in Javascript used to work with dates and times. 

Syntax:

new Date(year, month, date, hour, minute, second, millisecond)

Approach 1: Using this method, you can create a date with a set timezone. All you have to do is to use this inbuilt function by typing new Date() and then store the value in a new variable. Then display the contents of this variable using document.getElementById(“demo”).innerHTML. 

Example 1: 

HTML




<body>
    <h1 style="color:green">
        neveropen
    </h1>
      
    <p id="demo"></p>
      
    <script>
        var d = new Date(2019, 10, 4, 1, 43, 30, 0);
        document.getElementById("demo").innerHTML = d;
    </script>
</body>


Output: 

 

 Approach 2: Using Date() method, a new date is created and is stored in a variable. then using getTimezoneOffset() method we are able to add or subtract the timezone offset. 

Example 2: 

HTML




<body>
    <h1 style="color:green">
        neveropen
    </h1>
      
    <p id="demo"></p>
      
    <script>
        var d = new Date(2012, 9, 4);
        d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
        document.write(d);
    </script>
</body>


Output: 

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments