js diferentes métodos para obtener la fecha actual ayer, hoy, mañana

objetivo de aprendizaje:

学习目标

  • Obtener la fecha actual ayer, hoy, la fecha de mañana

Contenido de aprendizaje:

内容
1. Método
1. Obtener la fecha actual

  var today = new Date();  

2. Obtenga la fecha de ayer

  var yesterday = this.getDay(-1)

3. Obtenga la fecha de hoy

   var today = this.getDay(0)

5. Obtenga la fecha de mañana

   var tomorrow = this.getDay(1)

6. El código de muestra del método llamado es el siguiente:
Obtenga la fecha actual de ayer, hoy y mañana.

methods: {
        
            getDay(day) {  
                var today = new Date();  
                var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;  
                today.setTime(targetday_milliseconds); //注意,这行是关键代码
                  
                var tYear = today.getFullYear();  
                var tMonth = today.getMonth();  
                var tDate = today.getDate();  
                tMonth = this.doHandleMonth(tMonth + 1);  
                tDate = this.doHandleMonth(tDate);  
                return tYear + "-" + tMonth + "-" + tDate;
            },
            doHandleMonth(month) {  
                var m = month;  
                if (month.toString().length == 1) {  
                    m = "0" + month;  
                }  
                return m;
            },
        },

2. Método 2

1. Obtener la fecha actual

  var today = new Date();

2. Obtenga la fecha de ayer

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

3. Obtenga la fecha de hoy

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

4. Obtenga la fecha de mañana

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

Resumir:

知识小结:

Resumir:

  • Obtenga la fecha actual y calcule la fecha deseada
  {
            text: '本月',
            onClick(picker) {
                // 获取当前日期
                const today = new Date();
                // 获取当前月份的第一天
                const start = new Date(today.getFullYear(), today.getMonth(), 1);
                // 获取当前月份的最后一天
                const end = new Date(today.getFullYear(), today.getMonth() + 1, 0);
                picker.$emit('pick', [start, end]);
            }
          },
  • 1. Llame a este método pasando un valor.
 created() {
            console.log("昨天:", this.getDay(-1))
            console.log("今天:", this.getDay(0))
            console.log("明天:", this.getDay(1))
            console.log("20年以后:", this.getDay(20 * 365))
        }
  • Obtener la hora actual, nueva fecha()
  • el día es el número, getDay(-1): fecha de ayer; getDay(0): fecha de hoy; getDay(1): fecha de mañana;

Supongo que te gusta

Origin blog.csdn.net/YHLSunshine/article/details/132609217
Recomendado
Clasificación