我怎么知道下个生日的天数?

时间:2013-05-10 14:43:14

标签: javascript

我尝试了这个,但它失败了

var diffDays1=(function(){ 
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var secondDate = new Date(new Date().getFullYear()+1,4,5);
var firstDate = new Date();
 return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
})();

Wolfram alpha说它是330天,diffDays1显示它是359.这可能是由于夏令时或其他原因造成的。有没有办法准确计算天数,因为没有做服务器端。

5 个答案:

答案 0 :(得分:19)

问题在于,您将4月份定为4月份,当时4月份是Javascript中的3月份。见https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#Parameters

var diffDays1=(function(){ 
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    var secondDate = new Date(new Date().getFullYear()+1,3,5);
    var firstDate = new Date();
    return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
})();

答案 1 :(得分:4)

为什么重新发明轮子? 使用datejs

之后:

var startd = Date.parseExact(ind, "yyyy-MM-dd");
var endd = Date.parseExact(end, "yyyy-MM-dd");
var diff = new Date(endd - startd);
var days = diff/1000/60/60/24;

那是所有人!

答案 2 :(得分:2)

moment.js库(http://momentjs.com)处理这个以及许多其他JavaScript日期问题非常容易和很好。 diff函数(http://momentjs.com/docs/#/displaying/difference/)将完全按照您的意愿执行。

如果要显示从现在起你可以执行以下操作的天数,fromNow函数也非常好用:

moment([2014, 4, 5]).fromNow();
如果距离330天,

会产生类似“330天”的东西。

http://momentjs.com/docs/#/displaying/fromnow/

答案 3 :(得分:0)

如果生日是今年,则所选的解决方案将不起作用,因为它等于getFullYear的1。

这是我的解决方案,它还可以避免两个极端情况:今天的生日和剩余的1天。

const birthdayDay = 19;
const birthdayMonth = 11; // december === 11
const myBirthdayThisYear = new Date(new Date().getFullYear(), 11, 19).setHours(23, 59, 59);

export const daysUntilBirthday = () => {
  const addToYear = myBirthdayThisYear > Date.now() ? 0 : 1;
  const oneDay = 24 * 60 * 60 * 1000;
  const secondDate = new Date(new Date().getFullYear() + addToYear, birthdayMonth, birthdayDay);
  const firstDate = new Date();
  const days = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));
  const daysOrDay = days === 1 ? 'day' : 'days';
  return days !== 365 ? `${days} ${daysOrDay} until my birthday ` : ' TODAY IS MY BIRTHDAY ';
};

答案 4 :(得分:0)

这是一个使用矩的更清洁的解决方案,它可以正确处理所有情况(包括今天,今年或即将到来的生日,直到明年,时区,leap年等等):

const birthdate = '2018-12-15';
const today = moment().format('YYYY-MM-DD');
const years = moment().diff(birthdate, 'years');
const adjustToday = birthdate.substring(5) === today.substring(5) ? 0 : 1;
const nextBirthday = moment(birthdate).add(years + adjustToday, 'years');
const daysUntilBirthday = nextBirthday.diff(today, 'days');

简单,快速,准确!

这里是相同的代码,解释如下:

// This is the birthdate we're checking, in ISO 8601 format
const birthdate = '2018-12-15';

// Get today's date in ISO 8601 format
const today = moment().format('YYYY-MM-DD');

// Calculate current age of person in years (moment truncates by default)
const years = moment().diff(birthdate, 'years');

// Special case if birthday is today; we do NOT need an extra year added
const adjustToday = birthdate.substring(5) === today.substring(5) ? 0 : 1;

// Add age plus one year (unless birthday is today) to get next birthday
const nextBirthday = moment(birthdate).add(years + adjustToday, 'years');

// Final calculation in days
const daysUntilBirthday = nextBirthday.diff(today, 'days');

如果生日是今天,则结果为0;否则为0。如果是明天,结果将为1,依此类推。