PDA

View Full Version : Date Minus Two Script



FrickenTrevor
10-09-2009, 12:35 AM
I need a script that will print todays date minus two days
I found one in VB Script, but how can I make it JavaScript?

<script type="text/vbscript">
document.write(Date -2 & "<p> </p>")
</script>
Please keep this in JavaScript form and discuss.....

jscheuer1
10-09-2009, 02:07 AM
If this is one of those homework assignments type things and you just are too lazy to do the work, we cannot be specific. If not, please explain why you would want to do this.

That said:

Not knowing PHP well, I'm unclear as to exactly what you are after. However, in javascript the 'date' is the date number in the month - nothing more, nothing less. To get that number minus two, there can be two approaches. One would be to set a current Date Object back two days and and then retrieve the date number from it. This would allow for crossover into a previous month (where applicable) as the Date Object automatically compensates for that sort of thing. Like if you set a January 1st Date Object back 2 days, you get December 30th - the result would be 30. But if you merely took the date number of a current Date Object of January 1st and subtracted 2 from it, you would get -1.

For more information on how the Date Object works in javascript, see:

http://www.w3schools.com/jS/js_obj_date.asp

and:

http://www.w3schools.com/jsref/jsref_obj_date.asp

FrickenTrevor
10-09-2009, 02:37 AM
Well w3schools didnt realy help. Its too complicated.
Seconed, I just like saying


and discuss.....

to my posts. Its not homework of any kind.
I actualy need it for my site.

jscheuer1
10-09-2009, 02:52 AM
Please explain why you want to do this and I'd be happy to give you code for it, I've already written it out, at least as best as I could understand your requirements.

Did you at least understand my explanation, the difference between subtracting from the date number itself, as opposed to setting the Date Object back? Also, is this even what you are going for? Do you want the result as just a raw number -1 to 29, 1 to 31? Or what exactly? Why?