Domanda

So basically I have done a calculation that increments a clock time:

function Clock(year,month,day,hours,minutes,seconds){

  if(seconds !== null){
  this.seconds = seconds;
  }

  if(minutes !== null){
  this.minutes = minutes;
  }

  if(hours !== null){
  this.hours = hours;
  }

   if(day !== null){
  this.day = day;
  }

  if(month !== null){
  this.month = month;
  }

  if(year !== null){
  this.year = year;
  }

}


function incrementClock(){

clock.seconds++;
  if (clock.seconds >=60) {
    clock.seconds = 0;
    clock.minutes++;
    if (clock.minutes >=60) {
      clock.minutes = 0;
      clock.hours++;
      if (clock.hours >=24) {
        clock.hours = 0;
        clock.days++;
      }       
    }
  }    
}



function showClock(){

//prints clock in format yyyy/mm/dd hh:mm:ss

}

This would increment the seconds, minutes, hours and days accordingly...

So if I call print the clock each second it would look like this:

var c = new Clock(2014,04,01,12,13,01);

showClock();

2014/4/1 12:13:1

I get stuck on the month part...

My question is how would I go about checking if a month has passed as there are different amount of days each month?

EDIT

I am creating my own minified Date function... so please don't recommend using Date objects as I am trying to implement my own

È stato utile?

Soluzione

Is this what you're looking for?

function incrementClock(){
  clock.seconds++;
  if (clock.seconds >=60) {
    clock.seconds = 0;
    clock.minutes++;
    if (clock.minutes >=60) {
      clock.minutes = 0;
      clock.hours++;
      if (clock.hours >=24) {
        clock.hours = 0;
        clock.days++;
        var months = [31,((clock.year%4==0)&&((clock.year%100!=0)||(clock.year%400==0)))?29:28,31,30,31,30,31,31,30,31,30,31];
        if (clock.days>months[clock.month-1]){
          clock.days = 0;
          clock.months++;
        }
      }       
    }
  }    
}

This line:

var months = [31,((clock.year%4==0)&&((clock.year%100!=0)||(clock.year%400==0)))?29:28,31,30,31,30,31,31,30,31,30,31];

creates an array of the months and the days in each month. Thus it is easy to determine the amount of days per month like so:

days_in_jan = months[0]
days_in_feb = months[1]
...
days_in_dec = months[11]

Altri suggerimenti

You don't. You don't increment anything actually. If you want your own clock, that's fine, but you still need to access the current time using a Date object, so when you want to know how much time has elapsed, get a new Date object and figure it out:

pseudo structure:

Class Clock
    - start date

    - function timeElapsed
        - elapsed = now - start date

In that function, compare the month, day, etc.

And honestly, if you don't like that answer you're on your own, as the answer to "how do I do this random thing as purely a thought exercise" is exactly that -- a thought exercise. There's no real point to it, so it shouldn't be asked or answered on SO.

As others have done, I would suggest using the Date object, or a date library like MomentJS, but it seems like you want to do the implementation yourself as an exercise. In that case, your code will need to know how many days are in each month if it is going to do the calculation correctly. Consider including in your code an array of the months' lengths:

var monthLengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

You'll also need a function to determine whether or not it's a leap year:

function isLeapYear(year) {
  return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}  

Armed with those, you should be able to tell if a date has exceeded its month by checking the array to know how many days are in that month, and (if it's February) using the leap year function to determine if you should count 29 days for February.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top