469,293 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

code to show the date for yesterday

40
I'm pretty new concerning javascript.

I need to show the date for the day before today on a page.

I hacked at this code, and it works, but is there a more efficient way of achieving this?

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT type=text/javascript>
  2.  
  3. var mydate= new Date()
  4. mydate.setDate(mydate.getDate()-1)
  5.  
  6. var theyear=mydate.getFullYear()
  7. var themonth=mydate.getMonth()+1
  8. var theyesterday=mydate.getDate()
  9.  
  10. document.write("Data as of COB: ")
  11. document.write(themonth+"/"+theyesterday+"/"+theyear)
  12.  
  13. </SCRIPT>
  14.  
TIA,

Joe
Aug 1 '06 #1
5 31718
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT type=text/javascript>
  2. var month = new Array();
  3. month[0] = "January";month[1] = "February";month[2] = "March";month[3] = "April";month[4] = "May";
  4. month[5] = "June";month[6] = "July";month[7] = "August";month[8] = "September";month[9] = "October";month[10] = "November";
  5. month[11] = "December";
  6. //Array starting at 0 since javascript dates start at 0 instead of 1
  7. var mydate= new Date()
  8. mydate.setDate(mydate.getDate()-1)
  9. document.write("Date as of COB: "+(mydate.getMonth()+1)+"/"+mydate.getDate()+"/"+mydate.getFullYear());
  10. document.write("<br>"+month[mydate.getMonth()]);
  11. </SCRIPT>
  12.  
this would be one of the ways i would do it...
Aug 1 '06 #2
jej1216
40
I tried your script and couldn't get output.

Do you see anything in my script that is inherently wrong? It seems to be working, but I want to make sure I'm not ready to step into a problem. I had an earlier bit of code that worked until I crossed over to a new month - at which point it showed yesterday to be '8/0/2006', and the code I posted fixed that.

TIA,
Aug 2 '06 #3
jej1216
40
Oops - I didn't Cut/Paste your code correctly - it works! My apologies.

Now, to further my understanding, which code is preferable? Thye both seem to work, but is using an array a better approach?

Thanks,
Aug 2 '06 #4
iam_clint
1,208 Expert 1GB
All the array is for is something i added for you to get the actual name of the month.

its up to your preferences yes yours works fine.

the only thing i changed is the way ot displays. instead of writing it all to variables first i just have it write the date script..Depending on what your going to be applying this to aka if you need variables or not.

in my script to get the current name of the month would be

month[mydate.getMonth()]

month is the array i made and it starts with 0 cause javascript starts the months with 0 for some reason? so january is 0.
Aug 2 '06 #5
var yesterday= new Date()
yesterday.setDate(yesterday.getDate()-1)
("Yesterday's Date & Time:\n "+ yesterday.toLocaleString());
Oct 1 '08 #6

Post your reply

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

Similar topics

4 posts views Thread by necromancer_stupidspambot | last post: by
16 posts views Thread by Donnal Walter | last post: by
4 posts views Thread by Manny Chohan | last post: by
15 posts views Thread by Paul J. Ettl | last post: by
20 posts views Thread by WindAndWaves | last post: by
8 posts views Thread by Anonmyous | last post: by
1 post views Thread by Joris De Groote | last post: by
2 posts views Thread by Thierry Lam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.