实现更新月份

在小程序,想要实现自动更新是哪个月份,并且是用英文表示可以怎么实现呢?
有三种方法可以实现,具体如下

第一种:使用数组

1.在data中定义一个变量currentMonthInEnglish,用于存放当前月份的英文
2.定义一个数组monthNames,在数组里放入1-12月的英文
3.因为使用date的getmonth函数获取当前月份也是从0开始的,所以可以直接monthNames[now.getMonth()]
4.进行挂载
具体实现代码如下

<script>  
  export default {  
    data() {  
      return {  
        currentMonthInEnglish: '',  
 
      };  
    },  
    methods: {  
      formatDate() {  
        const now = new Date();  
        const monthNames = ["January", "February", "March", "April", "May", "June",  
                            "July", "August", "September", "October", "November", "December"];  
    
        const month = monthNames[now.getMonth()];  
       

        this.currentMonthInEnglish = month;  
      
      },  
  
    },  
    mounted() {  
      this.formatDate();  
    }  
  }  
</script>

第二种:使用switch

1.先定义一个变量e_month接收当前月份
2.定义变量获取getMonth函数所获取的当前月份
3.编写switch,用于根据当前是哪个月,就给e_month写入哪个月份的英文,也一样记得方法的挂载
具体实现代码如下

 data() { 
  e_month:""
 }
   methods: {
			// 月份英文
			get_E_month(){
				const now = new Date();  
				const currentMonth = now.getMonth() + 1; 
				// 输出1-12月英文
				switch(currentMonth){
					case 1:
						this.e_month="January";
						break;
					case 2:
						this.e_month="February";
						break;
					case 3:
						this.e_month="March";
						break;
					case 4:
						this.e_month="April";
						break;
					case 5:
						this.e_month="May";
						break;
					case 6:
						this.e_month="June";
						break;
					case 7:
						this.e_month="July";
						break;
					case 8:
						this.e_month="August";
						break;
					case 9:
						this.e_month="September";
						break;
					case 10:
						this.e_month="October";
						break;
					case 11:
						this.e_month="N0vember";
						break;
					case 12:
						this.e_month="December";
						break;
					
				}
				return currentMonth;
			}
   }

第三种:直接获取英文,但前提是使用的系统是英文模式

具体代码如下

export default {  
  data() {  
    return {  
      currentDate: '',  
    };  
  },  
  onLoad() {  
    this.getCurrentDate();  
  },  
  methods: {  
    getCurrentDate() {  
      const now = new Date();  
      const month = now.toLocaleString('en-US', { month: 'long' }); // 获取月份的英文  
      this.currentDate = month  
    },  
  },  
};
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_52559388

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值