I need to get same day last year in javascript!

J

John

If anyone can help me out with a good way to do this in javascript I
would greatly appreciate it.

I need to compute three dates in javascript - all of which relate to a
given date, say todays date.

I need to calculate:
1.
same day last year(that is, if today is friday, i want the date of the
friday 52 weeks ago)
2.
same day last week( same as above but only 1 week ago)
3.
same date last year

thanks
 
L

Lee

John said:
If anyone can help me out with a good way to do this in javascript I
would greatly appreciate it.

I need to compute three dates in javascript - all of which relate to a
given date, say todays date.

I need to calculate:
1.
same day last year(that is, if today is friday, i want the date of the
friday 52 weeks ago)
2.
same day last week( same as above but only 1 week ago)
3.
same date last year

Sounds like a homework assignment to me.

var today=new Date();
var week=new Date().setDate(today.getDate()-7);
var dayyear=new Date().setDate(today.getDate()-7*52);
var dateyear=new Date().setYear(today.getFullYear()-1);
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
I need to calculate:
1.
same day last year(that is, if today is friday, i want the date of the
friday 52 weeks ago)


As it happens, 31 December this year will be a Friday. 52 weeks before
it is 2 January, of this year, making the fault obvious. Sometimes you
need to go back 53 weeks; and there are various possible ways of doing
that.

D.setDate(D.getDate()-7*52)

will do what you parenthetically ask for, for any Date Object D.

2.
same day last week( same as above but only 1 week ago)
D.setDate(D.getDate()-7)


3.
same date last year

Not necessarily possible. Which day will you want to reach by going
back a year from the end of next week? Of course, you could go to the
previous instance of the same date, which was 2000-02-29.


Read the newsgroup FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top