获取JavaScript内剩余的月份,星期和天数 [英] Get the number of remaning Months, weeks and days inside JavaScript

查看:69
本文介绍了获取JavaScript内剩余的月份,星期和天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个JavaScript函数,该函数将比较2个日期值(开始日期和现在),然后显示:-

I want to write a JavaScript function, which will compare 2 date values (startdate & now) and then show:-

  1. 剩余的几个月&周.

  1. the remaining months & weeks.

(如果开始日期是当月的),以显示剩余的星期&天.

if the start date in with in the current month, to show the remaining weeks & days.

(如果起始日期在当前星期中),以显示重命名日期.

if the start date is with in the current week, to show the renaming days.

现在我找到了这个脚本:

now I find this script :

var nurl = items[i].CounterStartDate.toString();
var countDownDate = new Date(nurl).getTime();


var now = new Date().getTime();

// Find the distance between now an the count down date
var distance = countDownDate - now;

// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

将显示剩余的天,小时,分钟和秒.但是我不确定如何修改它以匹配我的上述3点?

which will show the remaining days, hours, minutes and seconds. But I am not sure how I can modify it to match my above 3 points?

提示.我使用的日期值来自rest api,它将具有以下格式2019-05-24T23:00:00Z2018-06-20T23:00:00Z等.

Hint. i date values i am using comes from a rest api and it will have the following format 2019-05-24T23:00:00Z or 2018-06-20T23:00:00Z, etc..

推荐答案

我认为此功能可以完成您想要的事情.

I think this function will do what you want.

function datediff(date) {
   let d1 = date;
   let d2 = now = new Date();
   if (d2.getTime() < d1.getTime()) {
     d1 = now;
     d2 = date;
   }
   let yd = d1.getYear();
   let yn = d2.getYear();
   let years = yn - yd;
   let md = d1.getMonth();
   let mn = d2.getMonth();
   let months = mn - md;
   if (months < 0) {
     years--;
     months = 12 - md + mn;
   }
   let dd = d1.getDate();
   let dn = d2.getDate();
   let days = dn - dd;
   if (days < 0) {
     months--;
     // figure out how many days there are in the last month
     d2.setMonth(mn, 0);
     days = d2.getDate() - dd + dn;
   }
   let weeks = Math.floor(days / 7);
   days = days % 7;
   if (years > 0) return years + ' years' + (months > 0 ? ' and ' + months + ' months' : '');
   if (months > 0) return months + ' months' + (weeks > 0 ? ' and ' + weeks + ' weeks' : '');
   if (weeks > 0) return weeks + ' weeks' + (days > 0 ? ' and ' + days + ' days' : '');
   return days + ' days';
}

输出:

console.log(datediff(new Date('2018-04-24')))
5 days
console.log(datediff(new Date('2018-04-04')))
3 weeks and 4 days
console.log(datediff(new Date('2018-03-30')))
4 weeks and 2 days
console.log(datediff(new Date('2018-03-28')))
1 months
console.log(datediff(new Date('2018-03-20')))
1 months and 1 weeks
console.log(datediff(new Date('2017-12-03')))
4 months and 3 weeks
console.log(datediff(new Date('2017-02-03')))
1 years and 2 months
console.log(datediff(new Date('2018-05-12')))
1 weeks and 5 days

这篇关于获取JavaScript内剩余的月份,星期和天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆