开发者

bizarre javascript arithmetic behavior (yup... to be expected)

开发者 https://www.devze.com 2022-12-14 14:25 出处:网络
ok, I\'m writing a little code snippet to get the ISO date-format value for yesterday. code: var dateString = new Date();

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 t开发者_JAVA技巧o me why javascript acts like this? is there any explanation for why this happens?


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)


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(),.


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


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).


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);


var dateString = new Date();

var yesterday = dateString.getFullYear();

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

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


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. :)


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


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>


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

精彩评论

暂无评论...
验证码 换一张
取 消