html - JavaScript : displaying Yesterdays Date in JavaScript - Stack Overflow

I was trying to display Yesterday Date on click of the button , but why its showing Date as "2013-

I was trying to display Yesterday Date on click of the button , but why its showing Date as "2013-6-0" instead of 2013-05-31

Could anybody please tell me what i was doing wrong

<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{

var d = new Date();
var curr_date = d.getDate()-1;
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var yesterday = curr_year + "-" + curr_month + "-" + curr_date ;

document.write(yesterday);

}
</script>
</head>
<body>


<p id="demo">Click Button to Display Yesterday Date</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html> 

I was trying to display Yesterday Date on click of the button , but why its showing Date as "2013-6-0" instead of 2013-05-31

Could anybody please tell me what i was doing wrong

<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{

var d = new Date();
var curr_date = d.getDate()-1;
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var yesterday = curr_year + "-" + curr_month + "-" + curr_date ;

document.write(yesterday);

}
</script>
</head>
<body>


<p id="demo">Click Button to Display Yesterday Date</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html> 
Share asked Jun 1, 2013 at 17:29 user663724user663724 3
  • 1 d.getDate() is 1, and 1 - 1 = 0, so what do you expect if not 0? You have to subtract 1 from the entire date, not just from the day-inside-the-month part. – escargot agile Commented Jun 1, 2013 at 17:31
  • Why are you adding 1 to the month? – Explosion Pills Commented Jun 1, 2013 at 17:32
  • @ExplosionPills well that'd be the right thing to do if it weren't being done at the wrong time :-) – Pointy Commented Jun 1, 2013 at 17:32
Add a ment  | 

4 Answers 4

Reset to default 4

You should update and then reference the date from which you've subtracted 1 day:

var d = new Date();

d.setDate(d.getDate() - 1); // <-- add this to make it "yesterday"

var curr_date = d.getDate(); // <-- don't subtract 1 anymore
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();

DEMO

Your code simply takes a day number (like 1, 2, ...) and subtracts one from it. Why would you expect that to automatically roll the day back to the previous month?

You can generate new dates by subtracting milliseconds from a given date. Try this:

var today = new Date();
# subtract milliseconds representing one day from current date
var yesterday = new Date(today - 24*60*60*1000);
var today = new Date();
var yesterday = new Date();
yesterday.setDate(today.getDate()-1);

var yesterdayStr = yesterday.getFullYear() + "-" + (yesterday.getMonth()+1) + "-" + yesterday.getDate();
function displayDate()
{
    var today = new Date();
    today.setDate(today.getDate()-1);
    var yyyy = today.getFullYear().toString();
    var mm = (today.getMonth()+1).toString();         
    mm = mm.length==2?mm:"0"+mm;
    var dd = today.getDate().toString();
    dd = dd.length==2?dd:"0"+dd;
    var yesterday = yyyy+"-"+mm+"-"+dd;

    document.write(yesterday);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745058698a4608829.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信