How To Get Previous Month Date In JavaScript?


Share:
Published By: Admin, Published On: Nov 27, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to get previous month date example.We will check how to get previous month date. This is a short guide on get previous month date in javascript. Here you will learn how to get javascript previous month date. Let's get started with how to get previous month date in javascript.

Here i will give you many example how to get previous month date using javascript.

Example : 1
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>How To Get Previous Month Date In JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Get Previous Month Date In JavaScript? - MyWebtuts.com</h3>

        <script type="text/javascript">
            const prevMonth = new Date();
            prevMonth.setMonth(prevMonth.getMonth()-1);

            document.write(prevMonth.toLocaleString('en-US'));
        </script>
    </body>
</html>
Output :
10/27/2021, 10:35:30 AM
Example : 2
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>How To Get Previous Month Date In JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Get Previous Month Date In JavaScript? - MyWebtuts.com</h3>

        <script type="text/javascript">
               var makeDate = new Date();

            document.write('Original date:', makeDate.toLocaleString('en-US'));

            makeDate.setMonth(makeDate.getMonth() - 1);

            document.write('<br> After subtracting a month: ', makeDate.toLocaleString('en-US'));
        </script>
    </body>
</html>
Output :
Original date:11/27/2021, 10:36:52 AM
After subtracting a month: 10/27/2021, 10:36:52 AM

It will help you...

Share: