By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,494 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

date / time object in javascript, Qustion about day of the week

100+
P: 130
hello all

i am breaking my head over this and there isnt any good solution that i have saw til now, my problam is that i want to tell the date of a sertin day what was choosen

[HTML]<select id="7_day" name ="7days">
<option value="0">Sunday</option>
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
</selectt>[/HTML]
when the user choose one of this days i will be able to get the date of all the last 5 sunday's for example

the problam i am getting is when trying to minus 7 out of the:

Expand|Select|Wrap|Line Numbers
  1. today = new Date();
  2. alert(today.getDate()-7 )
this is fine as long as we are not at the age.
Expand|Select|Wrap|Line Numbers
  1. alert (today.getDate()-28)
will bring back minus
i dont want to calculate it as intgers i want to calculate it as dates is it possible?? in php i would use mktime but js dont have it :(
somthing like that :
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $date= date("Y-m-d");
  3. $day = explode("-",$date);
  4. $lastWeek= strftime("%Y-%m-%d",(mktime(0,0,0,$day[1],$day[2]-7,$day[0])));
  5. ?>
any idea??
Jan 17 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Use the setDate() method:
Expand|Select|Wrap|Line Numbers
  1. var today = new Date();
  2. var newDate = new Date();
  3. newDate.setDate(today.getDate()-7);
Jan 17 '08 #2

100+
P: 130
that return seconds since epoch, (progress :))
how can i convert it to string like this yyyy-mm-dd
Jan 17 '08 #3

acoder
Expert Mod 15k+
P: 16,027
that return seconds since epoch, (progress :))
how can i convert it to string like this yyyy-mm-dd
Just output a string using the get*** methods:
Expand|Select|Wrap|Line Numbers
  1. alert(newDate.getFullYear() + "-" + (newDate.getMonth()+1) + "-" + newDate.getDate());
Jan 17 '08 #4

100+
P: 130
thanks,
its all make sense now
Jan 17 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad it helped.
Jan 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.