469,636 Members | 1,511 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

130 100+
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>
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
5 1462
16,027 Expert Mod 8TB
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
130 100+
that return seconds since epoch, (progress :))
how can i convert it to string like this yyyy-mm-dd
Jan 17 '08 #3
16,027 Expert Mod 8TB
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
130 100+
its all make sense now
Jan 17 '08 #5
16,027 Expert Mod 8TB
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.

Similar topics

7 posts views Thread by Mick White | last post: by
11 posts views Thread by Dennis Marks | last post: by
26 posts views Thread by jshanman | last post: by
12 posts views Thread by Assimalyst | last post: by
13 posts views Thread by ahjiang | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.