用javascript计算时间的流逝

时间:2018-01-28 22:51:00

标签: javascript date

我正在编写一个程序来计算从出生那天到剩下的时间到下一个生日(JavaScript)的时间流逝。我不知道如何考虑时区和闰年,这就是我计算错误的原因 - 天和小时不匹配。

var year; //variable that stores the user's birthday year
var month; //variable that stores the user's birthday month
var day; //variable that stores the user's birthday day

var todayDate = new Date();//variable that stores today's date

var currentYear = todayDate.getFullYear();//variable that stores current year
var currentmonth = todayDate.getMonth();//variable that stores current month
var currentday = todayDate.getDay();//variable that store current day 
var birthday = new Date(year, month, day);// generate date of birthday
var differentdate = (todayDate.getTime() - birthday.getTime()) /1000;//diffrence in time between the date of birth and the current one
//var y = 31622400;//the number of seconds in a leap year
const x = 31557600;//the number of seconds in a year

var passyear = Math.floor(differentdate / x);//variable that stores the years that have passed
var passday = Math.floor((differentdate % x) / 86400);//variable that stores the days that have passed
var numhours = Math.floor(((differentdate % x) % 86400) / 3600);//variable that stores the hours that have passed
var numminutes = Math.floor((((differentdate % x) % 86400) % 3600) / 60);//variable that stores the minutes that have passed
var numseconds = Math.floor((((differentdate % x) % 86400) % 3600) % 60);//variable that stores the seconds that have passed
//The condition checking the next birthday;
if (currentmonth > month) {
var futureDate = new Date((currentYear + 1), month, day);

} else if (currentmonth === month) {
   if (currentday >= day) {
   futureDate = new Date((currentYear + 1), month, day);
   } else {
   futureDate = new Date(currentYear, month, day);
            }

} else {
  futureDate = new Date(currentYear, month, day);
         }
         ;

var nextbirth = (futureDate - todayDate) / 1000;
var nextday = Math.floor((nextbirth % x) / 86400);
var nexthours = Math.floor(((nextbirth % x) % 86400) / 3600);
var nextminutes = Math.floor((((nextbirth % x) % 86400) % 3600) / 60);
var nextseconds = Math.floor((((nextbirth % x) % 86400) % 3600) % 60);

1 个答案:

答案 0 :(得分:0)

以下是使用Moment.js库的实现。它不仅仅是少数几行:)但我发现这个代码更具可读性,即使差异函数仍然感觉有点笨拙。

var month = "01"; // january
var day = "15"; // 15
var year = "1975";

var birthdate = moment(month + ' ' + day + ' ' + year, "MM DD YYYY");
var now = moment();
var nextBirthday = moment(month + ' ' + day + ' ' + now.year(), "MM DD YYYY");
if (nextBirthday < now) {
  nextBirthday.add(1, 'year');
}

console.log('birth date: ' + birthdate.format('MM/DD/YYYY'));
console.log('now: ' + now.format('MM/DD/YYYY'));
displayTimeDifference(now, birthdate);

console.log('next birthday: ' + nextBirthday.format('MM/DD/YYYY'));
console.log('now: ' + now.format('MM/DD/YYYY'));
displayTimeDifference(nextBirthday, now);

function displayTimeDifference(firstDate, secondDate) {
  let d1 = firstDate.clone(),
    d2 = secondDate.clone(),
    years = d1.diff(secondDate, 'years'),
    months = d1.subtract(years, 'years').diff(d2, 'months'),
    days = d1.subtract(months, 'months').diff(d2, 'days'),
    hours = d1.subtract(days, 'days').diff(d2, 'hours'),
    minutes = d1.subtract(hours, 'hours').diff(d2, 'minutes'),
    seconds = d1.subtract(minutes, 'minutes').diff(d2, 'seconds');

  console.log('years:' + years + ' months:' + months + ' days:' + days + ' hours:' + hours + ' minutes:' + minutes + ' seconds:' + seconds);
}
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>