views:

219

answers:

10

ok, I'm writing a little code snippet to get the ISO date-format value for yesterday.

code:

var dateString = new Date();

var yesterday = dateString.getFullYear();

 yesterday += "-"+dateString.getMonth()+1;

 yesterday += "-"+dateString.getDate()-1;

The above code outputs 2009-111-23. It is clearly not treating dateString.getMonth() as an intiger and tacking 1 on to the end of it.

Does putting the "-"+ in front of dateString.getDate() cast getDate() into a string?

this works gets the desired result.

var dateString = new Date();

var yesterday = dateString.getFullYear() + "-";

 yesterday += dateString.getMonth()+1+ "-";

 yesterday += dateString.getDate()-1;
//yesterday = 2009-12-22

Although I don't really like the way it looks... whatever no big deal.

Can anyone explain to me why javascript acts like this? is there any explanation for why this happens?

+2  A: 

It doesn't work. Try it out on the first of any month, and you'll get it reporting "2009-12-0" as yesterday.

Try something like this:

var mydate = new Date();
mydate.setDate(mydate.getDate()-1);
document.write(mydate.getFullYear() + "-" + (mydate.getMonth()+1) + "-" + mydate.getDate() );
Kieveli
good call... i'll figure something out - valid or not. Why does it behave like that?you should probably post stuff like that under comments... right? (new to this site... not sure if that's common practice or not)
Derek Adair
+13  A: 

This is about associativity. + operator is left-associative, so

"-"+dateString.getMonth() + 1

is same as

("-"+dateString.getMonth()) + 1

Put parenthesis around the expression you want to be evaluated first:

"-" + (dateString.getMonth() + 1)
eed3si9n
A: 

Yes, "-" + dateString.getMonth() casts to a string because one of the arguments is a string. So then when you add the 1, it is just appended to the string. It's not bizarre -- that's how just about every dynamically typed language would work.

Using parentheses should work:

    yesterday += "-"+(dateString.getMonth()+1);

    yesterday += "-"+(dateString.getDate()-1);
Kaleb Brasee
A: 
var dateString = new Date();

var yesterday = dateString.getFullYear();

 yesterday += "-"+String(parseInt(dateString.getMonth())+1);

 yesterday += "-"+String(parseInt(dateString.getDate())-1);
Mez
A: 

You understand it correctly already -- Javascript evaluates the right side of the assignment first, sees the "-" character, and commits that everything else will be casted to a string value.

In your first code example, you could get what you wanted by using parentheses to prevent the premature cast, as in:

var dateString = new Date();
var yesterday = dateString.getFullYear();
yesterday += "-" + (dateString.getMonth() + 1);
yesterday += "-" + (dateString.getDate() - 1);

Of course, you still will have an issue where you report out days of the month that are zero -- getDate() isn't zero-indexed. :)

delfuego
+1  A: 

In a nutshell, JavaScript is weakly typed. This means that it doesn't determine whether the var is text or a number until runtime. Because of this the order of operations matters. Looks like other posters have talked all about the associativity.

Remember, JavaScript is a functional language not an object-oriented one so there isn't casting as you know it (though I think there may be some utility functions to force JavaScript to treat something as a number - I can't rememeber off the top of my head).

dball917
You're thinking of `Number()`.
Matt Ball
+3  A: 

The correct way to get a date value representing "yesterday" is this:

var today = new Date();
var yesterday = new Date(today.getTime() - (1000*60*60*24));

From there you can get the values of interest, like yesterday.getDate(),.

Matt Ball
thanks - will this account for if it is the first of the month?
Derek Adair
Yes.What you're doing is getting "today" represented as milliseconds since the epoch, and subtracting off the number of milliseconds in a day, and using the Date constructor to do the rest of the work for you. This is a much better way to handle date arithmetic than trying to special-case everything yourself.
Matt Ball
awesome. very clever! +1
Derek Adair
A: 

As Kieveli mentions, this won't work on the first of the month. Instead, get the underlying number of milliseconds since the Epoch and subtract a day's worth:

var dateobj = new Date();
var yesterdayms = dateobj.valueOf() - (24*60*60*1000);
var yesterdayobj = new Date(yesterdayms);
var yesterdaydatestring = yesterdayobj.getFullYear() + "-"
                        + (yesterdayobj.getMonth()+1) + "-"
                        + yesterdayobj.getDate();
Phil H
A: 

Try this:

<script type="text/javascript">
var d = new Date();
document.writeln ("Today: " + d + "<br/>Yesterday:");
d.setDate(d.getDate()-1);
document.writeln (d);
</script>

EDIT

Or this:

<script type="text/javascript">
var d = new Date();
document.writeln ("Today: " +  (d.getDate()+1) + "-" + (d.getMonth()+1) + "-" + d.getFullYear() );
d.setDate(d.getDate()-1);
document.writeln ("<br/>Yesterday: " +  (d.getDate()+1) + "-" + (d.getMonth()+1) + "-" + d.getFullYear() );
</script>
graphicdivine
I need the ISO format: year-month-day
Derek Adair
I don't think this will work properly if it's the first of the month... could be wrong
Derek Adair
A: 

It would be more direct to get the date methods of yesterday, rather than subtract the values from today's date.

var d = new Date();
d.setDate(d.getDate()-1); //yesterday
var isodatestring= [d.getUTCFullYear(), 
d.getUTCMonth(),d.getUTCDate()].join('-');
kennebec