d = new Date(); d.setUTCFullYear( year ); d.setUTCMonth( month ); d.setUTCDate( date ); d.setUTCHours( 0 ); d.setUTCMinutes( 0 ); d.setUTCSeconds( 0 );
And everything worked fine, until last night when I would set
month=1 (February) but the month would end up as
2 (March). This had never happened before, and the code hadn't changed.
I finally realized that the date was 2016-01-28 St. Louis time at 2300, or 2016-01-29 UTC. So setting the year to a non-leap year like 2015, then setting the month to
setUTCMonth() meant I was trying to set it to 2015-02-29, which
Date helpfully corrected to 2015-03-01, then the date was set correctly. The only way that bug would turn up is if my code was run on the last day of the month and tested with a shorter month.
Turns out there's a much better way to set UTC time:
d = new Date( Date.UTC( year, month, date ) )
And now I am wiser.