Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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">
        GeeksforGeeks
    </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">
        GeeksforGeeks
    </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: 

 



Last Updated : 24 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads