vue-js实现日期加减,年月日,及单位换算。
1.点击年份加一
2.点击年份减一
3.点击月份加一
4.点击月份减一
5.点击天数加一
6.点击天数减一
7.checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’
8.时间转化函数。
1.转化日期函数(xx年xx月xx日xx时xx分xx秒)。
2.根据时间段获取相差XX天xx小时xx分钟
data() {return {yearMonthValue: "2021-12-23",};},
1.点击年份加一
// 点击年份加一YearAdd() {let currentDate = this.yearMonthValue;currentDate = new Date(currentDate);let nextDate = currentDate.setYear(currentDate.getFullYear() + 1); // 输出日期格式为毫秒形式1556668800000nextDate = new Date(nextDate);console.log(nextDate);this.yearMonthValue = nextDate;},
2.点击年份减一
// 点击年份减一YearReduce() {let currentDate = this.yearMonthValue;currentDate = new Date(currentDate);let lastDate = currentDate.setYear(currentDate.getFullYear() - 1); // 输出日期格式为毫秒形式1556668800000lastDate = new Date(lastDate);console.log(lastDate);this.yearMonthValue = lastDate;},
3.点击月份加一
// 点击月份加一MonthAdd() {let currentDate = this.yearMonthValue;currentDate = new Date(currentDate);let nextDate = currentDate.setMonth(currentDate.getMonth() + 1); // 输出日期格式为毫秒形式1556668800000nextDate = new Date(nextDate);let nextYear = nextDate.getFullYear();let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1nextDate = nextYear + "-" + nextMonth; // "2019-05"console.log(nextDate);this.yearMonthValue = nextDate;},
4.点击月份减一
// 点击月份减一MonthReduce() {let currentDate = this.yearMonthValue;currentDate = new Date(currentDate);let lastDate = currentDate.setMonth(currentDate.getMonth() - 1); // 输出日期格式为毫秒形式1551398400000lastDate = new Date(lastDate);let lastYear = lastDate.getFullYear();let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1lastDate = lastYear + "-" + lastMonth; // "2019-03"console.log(lastDate);this.yearMonthValue = lastDate;},
5.点击天数加一
//点击天数加一
DayAdd(){let currentDate = this.yearMonthValue;currentDate = new Date(currentDate);let nextDate = currentDate.setMonth(currentDate.getMonth()); // 输出日期格式为毫秒形式1556668800000nextDate = new Date(nextDate);let nextYear = nextDate.getFullYear();let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1let nextDay = currentDate.setDate(currentDate.getDate() + 1); // 输出日期格式为毫秒形式1556668800000nextDay = new Date(nextDay);let nextday = nextDay.getDate()nextDate = nextYear + "-" +nextMonth+"-"+nextday; // "2019-05"console.log(nextDate);this.yearMonthValue = nextDate;},
6.点击天数减一
//点击天数减一
DayReduce(){let currentDate = this.yearMonthValue;currentDate = new Date(currentDate);let nextDate = currentDate.setMonth(currentDate.getMonth()); // 输出日期格式为毫秒形式1556668800000nextDate = new Date(nextDate);let nextYear = nextDate.getFullYear();let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1let nextDay = currentDate.setDate(currentDate.getDate() - 1); // 输出日期格式为毫秒形式1556668800000nextDay = new Date(nextDay);let nextday = nextDay.getDate()nextDate = nextYear + "-" +nextMonth+"-"+nextday; // "2019-05"console.log(nextDate);this.yearMonthValue = nextDate;},
7.checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’
//checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’checkMonth(i) {if (i < 10) {i = "0" + i;}return i;},
转化日期函数(xx年xx月xx日xx时xx分xx秒)。
function filterTime(time) {var date = new Date(time);var y = date.getFullYear();var m = date.getMonth() + 1;m = m < 10 ? "0" + m : m;var d = date.getDate();d = d < 10 ? "0" + d : d;var h = date.getHours();h = h < 10 ? "0" + h : h;var minute = date.getMinutes();minute = minute < 10 ? "0" + minute : minute;var s = date.getSeconds();s = s < 10 ? "0" + s : s;return y + "年" + m + "月" + d + "日" + " " + h + ":" + minute;
}
根据时间段获取相差XX天xx小时xx分钟
// 根据时间段获取相差XX天xx小时xx分钟
function formatDuring(millisecond) {var days = parseInt(millisecond / (1000 * 60 * 60 * 24));var hours = parseInt((millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));var minutes = parseInt((millisecond % (1000 * 60 * 60)) / (1000 * 60));var seconds = (millisecond % (1000 * 60)) / 1000;return days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";
}
参考文档:【应用】如何使用JS实现日期按月份加减_Dora_5537的博客-CSDN博客_js月份加一
有用的话请点赞,关注加收藏哦(。・ω・。)ノ♡
vue-js实现日期加减,年月日,及单位换算。相关推荐
- js实现日期加减获取年龄
//js实现日期加减获取年龄var birthday=new Date('2021-01-01'.replace(/-/g, "\/")); //传入时间var d=new Dat ...
- js对日期加减指定天、时、分、秒
在前端编程中,经常需要对日期进行加减天.时.分.秒的操作,例如使用JS实现日期的倒计时.类似于C#中的AddDays.AddHours等,下面介绍在js中对日期加减的方法. 例如:当前日期为 2016 ...
- 使用js实现日期加减
使用js实现日期年月日的加减,自动处理闰年: function setDateTest() {addOrReduceDate("D","2019-6-15 14:45:1 ...
- js 日期加减操作(日、月、年)
js 日期加减操作(日.月.年) 先定义当前时间 const myDate = new Date() 2.日期加减操作 myDate.setYear(myDate.getFullYear() + 1) ...
- js日期加减一天_JS日期加减,日期运算代码
这篇文章主要介绍了JS日期加减,日期运算代码,需要的朋友可以参考下 一.日期减去天数等于第二个日期 function cc(dd,dadd){ //可以加上错误处理 var a = new Date( ...
- js日期加减一天_js日期如何进行加减计算
[摘要]首先介绍一下大的背景:就是我们的手机端项目涉及到购买会员之后,购买所有的商品都会有相应的折扣.那么我们的后台管理系统就可以指定用户,为其开通会员.核心技术:js日期如何进行加减计算. [作者] ...
- db2 日期加减一天_常用SQL系列之(八):列值累计、占比、平均值以及日期运算等...
本系统为@牛旦教育IT课堂在微头条上发布的内容,为便于查阅,特辑录于此,都是常用SQL基本用法. 前两篇连接: (一):SQL点滴(查询篇):数据库基础查询案例实战 (二):SQL点滴(排序篇):数据 ...
- JavaScript日期加减,Juqery日期加减计算并赋值给input框
开发中常用的日期加减法的处理方式 开发中遇到的日期加减的问题已经得到处理,现整理出来给需要的小伙伴参考 点击自定义时间按钮可以快速把需要的日期赋值给日期框和input框 1.获取当天的年月日 $(fu ...
- JavaScript日期加减
JS中的日期加减使用以下方式: varcurrentDate = new Date(); 对日期加减: date.setDate(date.getDate()+n); 对月加减: date.setMo ...
- java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)...
获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.ou ...
最新文章
- r 多元有序logistic回归_R语言多分类logistic逻辑回归模型在混合分布模拟单个风险损失值评估的应用...
- 10年Linux老司机吐血整理的命令大全,拿去吧
- 世界是一台超级计算机,这个世界其实是一个超级计算机
- 结对编程(黄金点游戏)
- c# 2.0实现摄象头视频采集,拍照,录象
- 简单计算机面试题库及答案_试讲可以看教案吗?必看的面试考前问题解答
- java 标记_Java中的标记语句块?
- nosql----redis持久化详解
- Python学习week2
- input重置为空后点击出现上次的值_上次玄乎的问题后续来了
- 054、JVM实战总结: 案例实战:每日百亿数据量的实时分析引擎,如何定位和解决频繁Full GC问题?
- python入门基础语法总结
- c# json 汉字乱码_json.net中文乱码问题
- notePad++安装及json,xml格式化插件安装
- 2022年3月份报告合集(共353份)
- IDEA提示“Spring Configuration Check“ “Unmapped Spring configuration files found.“
- javascript汉字转拼音代码
- Tumblr营销大法(一)
- 【面试】Tomcat面试题
- A-KAZE论文研读