js 计算两个日期之间 相差几年几月几日
1.计算日期差
Mine.vue
<!-- 我的 -->
<template>
<div>
<!-- 标题栏 -->
<x-header :left-options="{showBack: false}" title="计算时间差"></x-header>
<!-- 测试 -->
<group>
<x-input title="开始日期" placeholder="例如:20170101" v-model="startDate"></x-input>
<x-input title="结束日期" placeholder="例如:20180202" v-model="endDate"></x-input>
</group>
<div style="padding:15px;">
<x-button @click.native="monthDayDiff" type="primary">计算日期差</x-button>
</div>
<!-- 结果 -->
<group>
<x-input title="结果" v-model="result"></x-input>
</group>
</div>
</template> <script>
import { XHeader, Group, XInput, XButton } from 'vux' export default {
name: 'Mine',
components: {
XHeader,
Group,
XInput,
XButton,
},
data(){
return {
startDate: '20170101',
endDate: '20180202',
result: ''
}
},
methods: {
computedDayDiff(){
console.log(this.startDate);
console.log(this.endDate);
},
monthDayDiff() {
// this指针
let _this = this;
let flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2];
let start = new Date(this.changeFormat(_this.startDate));
let end = new Date(this.changeFormat(_this.endDate));
let year = end.getFullYear() - start.getFullYear();
let month = end.getMonth() - start.getMonth();
let day = end.getDate() - start.getDate();
if (month < 0) {
year--;
month = end.getMonth() + (12 - start.getMonth());
}
if (day < 0) {
month--;
let index = flag.findIndex((temp) => {
return temp === start.getMonth() + 1
});
let monthLength;
if (index <= 6) {
monthLength = 31;
} else if (index > 6 && index <= 10) {
monthLength = 30;
} else {
monthLength = 28;
}
day = end.getDate() + (monthLength - start.getDate());
}
// this.result = `相差${12 * year + month}月${day}天`;
this.result = `相差${year}年${month}月${day}天`;
},
changeFormat(str){
// 例如:将 20170101 改为 2017-01-01
return str.substr(0,4) + '-' + str.substr(4,2) + '-' + str.substr(6,2)
}
}
}
</script> <style lang="less" scoped>
//
</style>
2.效果图

js 计算两个日期之间 相差几年几月几日的更多相关文章
- 计算两个日期之间相差的年数月数天数(JS实现)
前言 如何计算年龄?我的第一直觉做法:(当前时间戳 - 出生时的时间戳)/ (365*86400) 所得结果向下取整.后来发现这种做法获得的结果不准确,不是多了一岁就是少了一岁,不能简单粗暴的这么处 ...
- JS计算两个日期之间的天数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd和YYYY-mm-dd HH:mm:ss
/** * 计算两个日期之间相差的天数(带带负数) 支持格式YYYY-mm-dd比较 * @param higDate 减数 * @param lowDate 被减数 * @returns 差值天数 ...
- tp5 计算两个日期之间相差的天数
//两个日期之间相差的天数 function diffBetweenTwoDays ($day1, $day2) { $second1 = strtotime($day1); $second2 = s ...
- PHP 求两个日期之间相差的天数、月数
<?php /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day ...
- js 求两个日期之间相差天数
//求两个日期之间的相差天数 function daysBetween(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne. ...
- php计算两个日期时间差(返回年、月、日)
在PHP程序中,很多时候都会遇到处理时间的问题,比如:判断用户在线了多长时间,共登录了多少天,两个帖子发布的时间差或者是不同操作之间的日志记录等等.在文章中,简单地举例介绍了PHP中如何计算两个日期相 ...
- js 计算两个日期之间的月数
//返回两个日期相差的月数 function MonthsBetw(date1, date2) { //用-分成数组 date1 = date1.split("-"); date2 ...
- JS计算两个日期之间的天数,时间差计算
1.日期之间的天数计算 //计算天数差的函数,通用 function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2017-9-25格式 var aDate, ...
随机推荐
- postman使用--发送请求
概述 上节讲了下接口的基础,从现在来学习怎么测接口.当然,测试接口有很多的工具,比如postman,jmeter等等,或者用代码测试,如果是做接口自动化我当然会选python,如果是调试接口,我特别喜 ...
- Bootstrap 3 Glyphicons are not working
Bootstrap 3 Glyphicons are not working 解答1 Note to readers: be sure to read @user2261073's comment a ...
- jquery实现密码强度检测
jquery实现密码强度验证 jquery实现密码强度验证 JS代码: $('#pass').keyup(function(e) { var strongRegex = new RegExp( ...
- RTSP详解
关于 RTSP. RTSP协议是一个非常类似HTTP协议的流控制协议.它们都使用纯文本来发送信息,而且rtsp协议的语法也和HTTP类似.Rtsp一开始这样设计,也是为了能够兼容使用以前写的HTTP协 ...
- CF550 DIV3
A - Diverse Strings CodeForces - 1144A A string is called diverse if it contains consecutive (adjace ...
- python 2018/8/25
# 含多空格字符串的分割 hello = "hello python hello"print(a.split(" ")) # ['hello', 'python ...
- npm start问题
问题:在执行命令npm start 是出现下列问题: npm [] WARN invalid config loglevel="notice" [] npm WARN invali ...
- Android开发——查询/杀死手机里正在运行的进程
0. 前言 以前有同学好像做过一个叫"自习君"的App,开启后自动检测用户这一天的自习时间,在学校里宣传广告还打了不少.其实实现原理非常简单,在SQlite数据库(也可以通过文件) ...
- UI进阶 XML解析适配 'libxml/tree.h'file not found 错误解决办法
Xcode 'libxml/tree.h'file not found 错误解决办法
- Selenium加载Chrome/Firefox浏览器配置文件
Selenium启动浏览器时,默认是打开一个新用户,不会加载原有的配置以及插件.但有些时候我们可能需要加载默认配置. 一.Chrome浏览器 1.在Chrome浏览器的地址栏输入:chrome://v ...