当前位置: 首页 > 知识库问答 >
问题:

用javascript计算时间流逝[重复]

端木宏盛
2023-03-14

我只是在写一个程序来计算从出生那天到下一个生日(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个答案

公冶经纶
2023-03-14

下面是一个使用矩的实现。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>
 类似资料:
  • 我已经创建了一个实时时钟,它与计算机时间同步,并显示在标签中。 记入NeverHopeless给出的起始代码。多谢了。

  • 问题内容: 我正在使用Android的软件包来开发包含多个的ViewPager 。 我正在尝试在用户更改页面时隐藏视图。因此,我实现了一个来监听适配器,并在发生页面更改时在s中调用一个方法。但是我得到了,我不知道为什么。 这是代码: 和 w当我复制/粘贴时,我删除了方法,但是它们在那里。该方法记录if ,并且始终返回false。当我寻呼时,被调用并返回true。为什么会发生这种情况,我该如何解决?

  • 我正试图在RoR上创建一个计算时间的应用程序。 当您按下开始按钮时,它会拉Time.now,然后,当您按下停止时,它会再次拉Time.now,然后计算两者之间的时间量。然后它会通过to_i将给定的秒转换为整数,然后将整数秒计算为小时:分钟:秒 然而,我的代码出了点问题,它不停地抛出一个又一个错误。 当前顺序为 nil:NilClass 的“未定义方法 '-'”

  • 这里的是带有白色背景的2px x 2px png,就是这样。 这个ffmpeg命令产生一个非常小的(文件大小)视频,它只是一个白色背景。 谁能解释一下我如何可以覆盖的图像作为一个延时视频在白色背景上使用FFMPEG?

  • 问题内容: ES6规范为键集合(Set,Map,WeakSet和WeakMap)提供什么时间复杂度(大O表示)? 我的期望,我期望的大多数开发人员,是规范和实现将使用被广泛接受的高性能算法,在这种情况下,并在平均情况下都是O(1)。这同样适用于和等效物。 对我来说,实现的时间复杂性是否在例如ECMAScript 2015 Language Specification-6th Edition — 2

  • 问题内容: 我正在尝试制作一个网页,当它开始加载时,使用一个间隔来启动计时器。 页面完全加载后,它将停止计时器, 但是即使有更长的时间,我有99%的时间获得了0.00或0.01的时间测量值。 有时,它说的东西有时更有意义,例如.28或3.10。 如果有帮助,请参见以下代码: 因此,它基本上会创建一个称为百分百的变量,该变量每10毫秒(.01秒)增加1。 然后,如果该数字达到1000(1整秒),则称