js ,html 获取当前时间,过去一个月,一年的时间(年-月-日)

//当前时间
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
//过去一个月的时间
function getPassFormatDate() {
    var nowDate = new Date();
    var date = new Date(nowDate);
    date.setDate(date.getDate()-30);
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

//过去一年的时间
function getPassYearFormatDate() {
    var nowDate = new Date();
    var date = new Date(nowDate);
    date.setDate(date.getDate()-365);
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue页面中添加选择查询数据份的下拉列表,支持当时间一年,可以按照以下步骤进行实现。 首先,在Vue组件的data中定义一个月份列表的数组变量,比如叫做months。接着,在Vue的created生命周期钩子函数中,通过循环遍历,获取当前时间一年的所有份,并将每个份的值和显示名称构成一个对象,然后将这个对象添加到months数组中。具体代码如下: ```javascript data() { return { months: [] } }, created() { let currentDate = new Date(); // 获取当前时间 for(let i = 0; i < 12; i++) { // 循环12个 let month = currentDate.getMonth() - i; // 获取份 let year = currentDate.getFullYear(); // 获取份 if(month < 0) { // 如果份小于0,表示跨了 month = 12 + month; // 校正份 year = year - 1; // 份减一 } let monthValue = year + '-' + (month + 1); // 构造份值,比如:2022-1 let monthName = year + '' + (month + 1) + ''; // 构造份显示名称,比如:20221 this.months.push({value: monthValue, name: monthName}); // 将份对象添加到数组中 } }, ``` 接着,在Vue的template中,使用`v-for`指令将months数组进行渲染,生成下拉列表的选项。代码如下: ```html <select> <option value="">请选择查询份</option> <option v-for="month in months" :value="month.value">{{ month.name }}</option> </select> ``` 最后,你可以根据需要为这个下拉列表添加相应的样式和事件处理逻辑即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值