SoFunction
Updated on 2025-03-10

js summary of different methods for getting the current date yesterday, today, and tomorrow date

1. Method 1

1. Get the current date

  var today = new Date();

2. Get yesterday's date

  var yesterday = (-1)

3. Get the date of today

   var today = (0)

5. Get the date of tomorrow

   var tomorrow = (1)

6. The method sample code of the called method is as follows:

Get the current date yesterday, today, tomorrow date

methods: {
        
            getDay(day) {
                var today = new Date();
                var targetday_milliseconds = () + 1000 * 60 * 60 * 24 * day;
                (targetday_milliseconds); //Note that this line is the key code                
                var tYear = ();
                var tMonth = ();
                var tDate = ();
                tMonth = (tMonth + 1);
                tDate = (tDate);
                return tYear + "-" + tMonth + "-" + tDate;
            },
            doHandleMonth(month) {
                var m = month;
                if (().length == 1) {
                    m = "0" + month;
                }
                return m;
            },
        },

2. Method 2

1. Get the current date

  var today = new Date();

2. Get yesterday's date

   today .setTime(()-24*60*60*1000);
   var yesterday = today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();

3. Get the date of today

   today .setTime(today .getTime());
   var day= today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();

4. Get the date of tomorrow

   today .setTime(today .getTime()+24*60*60*1000);
   var tomorrow= today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();

Summarize:

Knowledge summary:

Summarize:

  • Get the current date and calculate the desired date
  {
            text: 'this month',
            onClick(picker) {
                // Get the current date                const today = new Date();
                // Get the first day of the current month                const start = new Date((), (), 1);
                // Get the last day of the current month                const end = new Date((), () + 1, 0);
                picker.$emit('pick', [start, end]);
            }
          },
  • 1. Call this method by passing the value
 created() {
            ("yesterday:", (-1))
            ("today:", (0))
            ("tomorrow:", (1))
            ("Twenty years later:", (20 * 365))
        }
  • Get the current time, new Date()
  • day is number, getDay(-1): yesterday's date; getDay(0): today's date; getDay(1): tomorrow's date;

Attachment: js method to get the current day of the week

The Date object in JavaScript provides a way to get the current date and time. Among them, the getDay() method can return the current day of the week, with the return value of 0-6, representing Sunday to Saturday respectively.

Here is a sample code for getting the current day of the week using the Date object:

const weekDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const today = new Date();
const dayOfWeek = weekDays[()];
(`Today is ${dayOfWeek}`);

In the above code, we first define an array weekDays containing Sunday to Saturday. Then, we create a Date object, use the getDay() method to get the current star date, and use the array weekDays to get the corresponding day of the week name.

This is the end of this article about different methods of obtaining the current date yesterday, today, and tomorrow. For more related JS content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!