Bizarre arithmetic javascript behavior (yup ... can be expected)

ok, I am writing a small piece of code to get the ISO date format value for yesterday.

the code:

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

The above code outputs 2009-111-23. This does not explicitly apply to dateString.getMonth () as an intiger, and binds 1 to the end.

Does "-" + put getDate () in a string before dateString.getDate ()?

this leads to 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 ... no matter what happens.

Can someone explain to me why javascript works like this? are there any explanations why this is happening?

+4
source share
10 answers

It's about associativity . + the operator is left-associative, therefore

 "-"+dateString.getMonth() + 1 

coincides with

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

First put brackets around the expression you want to evaluate:

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

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

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

From there you can get values ​​of interest, for example yesterday.getDate()

+3
source

This does not work. Try this the first of any month, and you will receive a reporting message "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() ); 
+2
source

In short, JavaScript is weakly typed. This means that it does not determine if var is text or a number until runtime. Because of this, the order of operations matters. Other posters seemed to be talking about associativity.

Remember that JavaScript is a functional language, not an object-oriented one, so you are not casting as you know it (although I think there may be some utility functions to get JavaScript to process something like a number - I cannot remember from the head).

+1
source

Yes, "-" + dateString.getMonth() a string, because one of the arguments is a string. So, when you add 1, it just gets added to the line. This is not strange - almost all dynamically typed languages ​​would work.

Using parentheses should work:

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

You already understand this correctly - Javascript first evaluates the right-hand side of the job, sees the β€œ-” character and commits that everything else will be passed to a string value.

In your first code example, you can get what you want using parentheses to prevent premature playback, for example:

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

Of course, you will still have a problem when you report days of the month that are zero - getDate() not indexed by zero. :)

0
source

As Kiveli mentions, this will not work in the first month. Instead, get the base number of milliseconds since the Age and subtract the cost of the day:

 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(); 
0
source

Try the following:

 <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 that:

 <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> 
0
source

It would be better to get yesterday's methods, rather than subtract the values ​​from today.

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

All Articles