25

I've been looking for a while to get yesterday's date in format DD/MM/YYYY. Here's my current code:

var $today = new Date();
var $dd = $today.getDate();
var $mm = $today.getMonth()+1; //January is 0!

var $yyyy = $today.getFullYear();
if($dd<10){$dd='0'+dd} if($mm<10){$mm='0'+$mm} $today = $dd+'/'+$mm+'/'+$yyyy;

With this, I get today's date in format DD/MM/YYYY (thanks SO). But when I try this:

var $yesterday = $today.getDate()-1;

as recommended on this site somewhere else (lost the link), I get an error saying that getDate() was not found for this object.

I'm using my script with Sahi, but I don't think it's linked, as Sahi has no trouble with Javascript.

Thank you in advance.

Joe
  • 434
  • 1
  • 4
  • 15
  • Would this be better: http://jsfiddle.net/LZLgN/ ? Of course you'll have to fill necessary `0`s. – Passerby May 22 '13 at 08:16
  • 1
    It's because your `$today` was finally assigned to a string (`"dd/mm/yyyy"`), and a string do not have method `getDate()`. – Passerby May 22 '13 at 08:19
  • 5
    Why all the awkward `$`? It's not PHP, you know. – Nikita Volkov May 22 '13 at 08:25
  • 2
    Just FYI, there's a great library to handle Date manipulation: [Moment.js](http://momentjs.com). – ldiqual May 22 '13 at 17:47
  • this wont work on 1st Jan, following would: `function getYesterdayDate() { let yesterday = new Date(new Date().getTime() - 24 * 60 * 60 * 1000); let arr = []; arr.push(yesterday.getDate() < 10 ? '0' + yesterday.getDate() : yesterday.getDate()); arr.push(yesterday.getMonth() + 1 < 10 ? '0' + (yesterday.getMonth() + 1) : yesterday.getMonth() + 1); arr.push(yesterday.getFullYear()); return arr.join('-'); }` – Ankit Mar 24 '23 at 08:37

3 Answers3

64

The problem here seems to be that you're reassigning $today by assigning a string to it:

$today = $dd+'/'+$mm+'/'+$yyyy;

Strings don't have getDate.

Also, $today.getDate()-1 just gives you the day of the month minus one; it doesn't give you the full date of 'yesterday'. Try this:

$today = new Date();
$yesterday = new Date($today);
$yesterday.setDate($today.getDate() - 1); //setDate also supports negative values, which cause the month to rollover.

Then just apply the formatting code you wrote:

var $dd = $yesterday.getDate();
var $mm = $yesterday.getMonth()+1; //January is 0!

var $yyyy = $yesterday.getFullYear();
if($dd<10){$dd='0'+$dd} if($mm<10){$mm='0'+$mm} $yesterday = $dd+'/'+$mm+'/'+$yyyy;

Because of the last statement, $yesterday is now a String (not a Date) containing the formatted date.

Sam
  • 40,644
  • 36
  • 176
  • 219
  • Thank you. Does this method work for every day of the year? – Joe May 22 '13 at 08:35
  • @user1881815, I'm pretty sure it will. I tested it for the first day of the year, the first day of a month, and the current date, and it worked in those cases. – Sam May 22 '13 at 08:38
21

Try this:

function getYesterdaysDate() {
    var date = new Date();
    date.setDate(date.getDate()-1);
    return date.getDate() + '/' + (date.getMonth()+1) + '/' + date.getFullYear();
}
katranci
  • 2,551
  • 19
  • 24
3

You override $today in the if statement.

if($dd<10){$dd='0'+dd} if($mm<10){$mm='0'+$mm} $today = $dd+'/'+$mm+'/'+$yyyy;

It is then not a Date() object anymore - hence the error.

madflow
  • 7,718
  • 3
  • 39
  • 54