3

I want to display yesterday’s date by using JavaScript. For that, I use code as follows:

<p id="demo"></p>

<script type=text/javascript>
    var d = new Date();
    d.setDate(d.getDate() - 1);
    document.getElementById("demo").innerHTML = d;
</script>

It works, but displays the date in complete format with time:

Sun May 01 2016 11:31:53 GMT-0700 (Pacific Daylight Time)

I want to show only day and date format like this:

Sun May 01 2016

Is there any solution for this?

dakab
  • 5,379
  • 9
  • 43
  • 67
Manoj
  • 69
  • 1
  • 3
  • 11

6 Answers6

2

If you are flexible to use external libraries, you can use moment.js.

document.write(moment().add(-1, "days").format("ddd MMM DD YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
Rajesh
  • 24,354
  • 5
  • 48
  • 79
1
var d = new Date();
    d.setDate(d.getDate() - 1);
    var weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

    var dayName = weekday[d.getDay()];
    var month = months[d.getMonth() ];
    var day = d.getDate();
    var year = d.getFullYear();

    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;

    var output = dayName + ' ' + month + ' ' + day + ' ' + year;
karthik
  • 74
  • 9
  • 21
  • 45
1

getDay() return 0-6 .getMonth() return 0-11 so create your custom array name for that in right position.Then you can call it..

<p id="demo"></p>
<script type=text/javascript>
var d = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var month = ["January", "February", "March","April", "May", "June", "July","August", "September", "October",
            "November", "December"];

d.setDate(d.getDate() - 1);
document.getElementById("demo").innerHTML = days[d.getDay()]+" "+month[d.getMonth()]+" "+(d.getDate()<10?"0"+d.getDate():d.getDate())+" "+d.getFullYear();
</script>
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
0

Find the below code to format date in dd/mm/yyyy format.

function formatDate(d) {

   if (!d) return null;

   var date = d.getDate(), month = d.getMonth(), year = d.getFullYear();

  return addZero(date)+"/"+addZero(month+1)+"/"+year;

}
function addZero(val) {
  return val<10? "0"+val: val;
}

var d = new Date();
d.setDate(d.getDate() - 1);
formatDate(d);
Srinu Chinka
  • 1,471
  • 13
  • 19
0

You basically have following functions:

getMonth()
getDay()
getFullYear()
getDate()

so you can use this to format the date in your own way. Here is the example:

var day = {"sunday","monday","tuesday","wednesday","thrusday","friday","saturday"};

var date = new Date();
date.setDate(date.getDate()-1);
var a=date.getDay();
var b=date.getMonth();
var c=date.getDate();
var d=date.getFullYear();

console.log(day[a]+" "+b+" "+c+" "+d);

If you want to format the dates and times in more appropriate way, I suggest you to use moment.js.

uvishere
  • 455
  • 6
  • 22
0

you can use the substring

<p id="demo"></p>
<script type='text/javascript'>
var d = new Date();
d.setDate(d.getDate() - 1);
d = d.toString().substring(0,15);
document.getElementById("demo").innerHTML = d;//Sun May 01 2016
</script>
Coco Tsau
  • 33
  • 4