How To Convert A Date To GMT In JavaScript

Convert a Date to GMT in JavaScript

This article can help you learn about how to convert a Date to GMT in JavaScript. Follow this article to learn more about it with the explanation and examples below.

Convert A Date To GMT In JavaScript

GMT (abbreviated from English Greenwich Mean Time, often referred to as GMT for “Greenwich Mean Time”) is the solar time at the Royal Observatory Greenwich in Greenwich near London, England. This place is conventionally located on the zero meridians.

These are some solutions that can help you convert a Date to GMT in JavaScript. 

Use the toUTCString() method

You can use the toUTCString() method built-in into the Date object to convert a Date to GMT.

This method returns a string that represents the date according to UTC.

Syntax:

Date.toUTCString()

Parameters: None

Return value: A string that represents the UTC date and time.

Look at the example below to learn more about this method.


<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Convert A Date To GMT In JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: black;">
        How To Convert A Date To GMT In JavaScript
    </h1>
    <h2 style="color:blue;">
        Convert A Date To GMT In JavaScript
    </h2>
    <h3>
        Use the toUTCString() method
    </h3>
    <script>
        document.writeln("The current date: ");
        const date = new Date();
        document.write(date);
        document.write("<br>The updated date: "+date.toUTCString())

    </script>
</body>
</html

Output

Get the specified GMT time

Besides converting a Date to GMT, you can get the specified GMT by functions built-in in the Date object. You can get the UTC Date, Hours, Minutes, etc.

Look at the example below to learn more about this solution.

<!DOCTYPE html>
<html>
    <head>
        <title>
            How To Convert A Date To GMT In JavaScript
        </title>
    </head>
<body style="text-align:left;">
    <h1 style="color: black;">
        How To Convert A Date To GMT In JavaScript
    </h1>
    <h2 style="color:blue;">
        Convert A Date To GMT In JavaScript
    </h2>
    <h3>
        Get the specified GMT time
    </h3>
    <script>
        document.writeln("The current date: ");
        const date = new Date();
        document.write(date);

        // Get the specified GMT by functions built-in in the Date object.
        document.write("<br>The UTC Year: "+date.getUTCFullYear())
        document.write("<br>The UTC Month: "+date.getUTCMonth())
        document.write("<br>The UTC Day: "+date.getUTCDate())
        document.write("<br>The UTC Hours: "+date.getUTCHours())
        document.write("<br>The UTC Minutes: "+date.getUTCMinutes())
        document.write("<br>The UTC Seconds: "+date.getUTCSeconds())

    </script>
</body>
</html

Output

Summary

These are some methods that can help you convert a Date to GMT in JavaScript. To do this, you can use the toUTCString() method or you can get the specified GMT time by functions built-in in the Date object. Choose the method that is the most suitable for you. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *