Last day of the month (no timestamp)

I am trying to create two dynamic dates in html / javascript / jquery. I want dates to be formatted as yyyy / mm / dd. The first date will return the last day of the previous month, and the second date will return the last day of the current month. Does anyone know how to do this in the above technologies?

I am at 99.9999%. I can accomplish this by accessing the backend page using C #, but I want to see if there is a more efficient way to do this directly in the DOM (is the DOM the correct terminology?).

From the previous question about SO, here is the code I'm using now ...

var d = new Date();
document.write(new Date(d.getFullYear(), d.getMonth() + 1, 0, 23, 59, 59));
+3
source share
3 answers

It seems you already have most of the answer:

var d = new Date();
var lastcurrent = new Date(d.getFullYear(), d.getMonth() + 1, 0, 23, 59, 59);
var lastprevious = new Date(d.getFullYear(), d.getMonth(), 0, 23, 59, 59);

, , , :

document.write(lastcurrent.getFullYear() + '/'
               + (lastcurrent.getMonth() + 1) + '/' + lastcurrent.getDate());
document.write(lastprevious.getFullYear() + '/' 
               + (lastprevious.getMonth() + 1) + '/' + lastprevious.getDate());

jsFiddle .

Edit
( ):

document.write(lastcurrent.getFullYear()
               + '/' + String('00'+(lastcurrent.getMonth() + 1) ).slice(-2)
               + '/' +  lastcurrent.getDate() );
document.write(lastprevious.getFullYear()
               + '/' + String('00'+ (lastprevious.getMonth() + 1) ).slice(-2)
               + '/' + lastprevious.getDate());
+4

var today = new Date();

var lastofpreviews = new Date( today.getUTCFullYear(), today.getUTCMonth()    , 0 );
var lastofthis     = new Date( today.getUTCFullYear(), today.getUTCMonth() + 1, 0 );

demo at http://jsfiddle.net/gaby/GW9B9/

0
var d = new Date();
document.write(new Date(d.getFullYear(), d.getMonth(), 0, 23, 59, 59).toLocaleFormat('%Y/%m/%d'));
document.write(new Date(d.getFullYear(), d.getMonth() + 1, 0, 23, 59, 59).toLocaleFormat('%Y/%m/%d'));

Format dates in the requested format (YYYY / mm / dd)

0
source

All Articles