469,602 Members | 1,829 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

I need to get same day last year in javascript!

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
Jul 20 '05 #1
2 9066
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);

Jul 20 '05 #2
JRS: In article <66************************@posting.google.com>, seen
in news:comp.lang.javascript, John <my********@yahoo.com> posted at Fri,
20 Feb 2004 08:58:53 :-
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Amanda | last post: by
8 posts views Thread by timmy_dale12 | last post: by
2 posts views Thread by Mike Button | last post: by
7 posts views Thread by laredotornado | last post: by
reply views Thread by U S Contractors Offering Service A Non-profit | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.