vue js获取当前时间,年月日时分秒

效果图:在这里插入图片描述
话不多说,直接看代码:

    var date = new Date();
    this.year = date.getFullYear();//年
    this.month = date.getMonth() + 1;//月
    this.strDate = date.getDate();//日
    if (this.month >= 1 && this.month <= 9) { this.month = "0" + this.month;}
    if (this.strDate >= 0 && this.strDate <= 9) {this.strDate = "0" + this.strDate;}
    console.log(this.year+'-'+this.month+'-'+this.strDate)

在mounted中直接定义一个即可,也可以在methods中写一个时间方法,哪里要用直接调用那个方法即可,都行,看个人喜好,
如果还需要时分秒在后面加就可以了,
时:getHours() (0-23)
分:getMinutes() (0-59)
秒:getSeconds() (0-59)
。。。
时分秒都是跟月份一样,从0开始数的,不用+1,因为月是1-12月,而时分秒是0-23和0-59。没了,结束了,是不是很简单呐
最后:如果此篇博文对您有帮助,还请动动小手点点关注点点赞呐~,谢谢 ~ ~

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

杨同学*

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

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

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

打赏作者

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

抵扣说明:

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

余额充值