jsで現在の日付、昨日、今日、明日の日付を取得するさまざまな方法

学習目標:

学习目标

  • 昨日、今日、明日の現在の日付を取得します

学習内容:

内容
1. 方法
1. 現在の日付を取得する

  var today = new Date();  

2. 昨日の日付を取得する

  var yesterday = this.getDay(-1)

3. 今日の日付を取得する

   var today = this.getDay(0)

5. 明日の日付を取得する

   var tomorrow = this.getDay(1)

6. 呼び出されるメソッドのサンプルコードは次のとおりです。
現在の日付の昨日、今日、明日の日付を取得します。

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.方法2

1. 現在の日付を取得する

  var today = new Date();

2. 昨日の日付を取得する

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

3. 今日の日付を取得する

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

4. 明日の日付を取得する

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

要約:

知识小结:

要約:

  • 現在の日付を取得し、希望の日付を計算します
  {
            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. 値を渡してこのメ​​ソッドを呼び出します
 created() {
            console.log("昨天:", this.getDay(-1))
            console.log("今天:", this.getDay(0))
            console.log("明天:", this.getDay(1))
            console.log("20年以后:", this.getDay(20 * 365))
        }
  • 現在時刻を取得 new Date()
  • day は数値です、getDay(-1): 昨日の日付; getDay(0): 今日の日付; getDay(1): 明日の日付;

おすすめ

転載: blog.csdn.net/YHLSunshine/article/details/132609217