Function to get yesterday's date in Javascript in format DD/MM/YYYY -


this question has answer here:

i've been looking while yesterday's date in format dd/mm/yyyy. here's current code:

var $today = new date(); var $dd = $today.getdate(); var $mm = $today.getmonth()+1; //january 0!  var $yyyy = $today.getfullyear(); if($dd<10){$dd='0'+dd} if($mm<10){$mm='0'+$mm} $today = $dd+'/'+$mm+'/'+$yyyy; 

with this, today's date in format dd/mm/yyyy (thanks so). when try this:

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

as recommended on site somewhere else (lost link), error saying getdate() not found object.

i'm using script sahi, don't think it's linked, sahi has no trouble javascript.

thank in advance.

the problem here seems you're reassigning $today assigning string it:

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

strings don't have getdate.

also, $today.getdate()-1 gives day of month minus one; doesn't give full date of 'yesterday'. try this:

$today = new date(); $yesterday = new date($today); $yesterday.setdate($today.getdate() - 1); //setdate supports negative values, cause month rollover. 

then apply formatting code wrote:

var $dd = $yesterday.getdate(); var $mm = $yesterday.getmonth()+1; //january 0!  var $yyyy = $yesterday.getfullyear(); if($dd<10){$dd='0'+dd} if($mm<10){$mm='0'+$mm} $yesterday = $dd+'/'+$mm+'/'+$yyyy; 

because of last statement, $yesterday string (not date) containing formatted date.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -