Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Algorithm

Hello,
Need advise on writing the  an algorithm for the spec below:
Return 3 digit integer as day month year by adding  an number to a given date
Example date given is 1-1-2019 and integer to be added is 40 . Also just need the logic without any built in functions.
Any suggestions?
Thanks
Avatar of d-glitch
d-glitch
Flag of United States of America image

Your specification doesn't make any sense.
Try again with some examples.
I hate mind reading.

But are you supposed to return the date that 40 days after 1-1-2019?    I note that 40 is not a three digit number.

Does it have to work for 999 which is a three digit number?   If so, do you have to worry about Leap Years?

Is this what you mean:   FromNow( 1-18-2019, 366) = 1-19-2020
Avatar of RIAS

ASKER

Hello,
Example:

Given a date : 1-January-2019
I need to add a number to it , suppose 40
I need to output it as days , month and year

Suppose 1-January-2019, I add a day to it the answer is 2-January -2019
Maybe you want a function that returns the date given an input date and number of days? In JavaScript that is:

const myDate = new Date();
const numberOfDaysToAdd = 40;
myDate.setDate(myDate.getDate() + numberOfDaysToAdd); 
const myFormattedDate = myDate.getDate() + '/'+  myDate.getMonth() + 1 + '/'+ myDate.getFullYear();
console.log(myFormattedDate);

Open in new window


I would use the moment.js library personally.
Avatar of RIAS

ASKER

Hello Micahel,
Thanks. Can you suggest without adding any functions. Just need a logic algo.
It's really not clear what you are looking for. I think you're going to have to provide some more information and maybe examples.

Are you looking for JavaScript code? Java code? What do you mean by "logic algo"?
This sounds like some sort of homework assignment.

To develop the logic, you should sit down with a calendar and do several examples by hand.
Take notes of what you are doing, and the information you need.  
Things like how many days are in each month.
Avatar of RIAS

ASKER

Thanks. I am looking a logic flowchart and not functions like getDate().
d-glitch - ts not a homework assigment.  I am trying to write a logic flowchart without any inbuilt functions.
Really want something like a flowchart which comes before you code.
One approach to the problem would be to step forward, one day at a time.
You increment the day of the month [and roll over months and years as required] and decrement the number of days until you get to zero.

This would not be efficient.  You could try stepping to the 1st of the next month repeatedly, until you run out of days.
Avatar of RIAS

ASKER

Will not adding 1 to days be the starting point.
Then check whether the days exceed the month days .?
ASKER CERTIFIED SOLUTION
Avatar of d-glitch
d-glitch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Yes I agree. Thanks
Avatar of RIAS

ASKER

Hi ,
Will you be able to show this in a for loop ?

Thanks
Yes.  
Increment Day = Day +1

Check to see if Month rolls over.  
   No ==> end of loop
  Yes ==> Day = 1    Increment Month = Month + 1

Check to see if Year rolls over.  
   No ==> end of loop
  Yes ==> Month = 1    Increment Year = Year + 1
  ==> end of loop

Checking the end of Month is tricky because Months are different sizes with the added complication of Leap Year.
Avatar of RIAS

ASKER

Thanks,
Can you suggest if I add 40 to todays date what will the output be?
Day 41 is Feb 10th   2-10-2019
Avatar of RIAS

ASKER

Thanks!!!