Reputation: 4685
I have the following code to get the current day:
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
var d = new Date();
var curr_date;
if (d.getDate() == 1)
{
curr_date = d.getDate();
}
else
{
curr_date = d.getDate() - 1;
}
var curr_month = d.getMonth() + 1; //months are zero based
var curr_year = d.getFullYear();
var message_day = curr_year + "_" + curr_month + "_" + curr_date;
var sql_day = month[curr_month - 1].substring(0,3) + curr_date.toString() + curr_year.toString();
if (curr_date == "1")
{
document.write(sql_day + " " + message_day);
}
This is great for getting the current day, but now i need to get the last day of the last month if it is the beginning of a new month.
right now this will produce:
Feb12012 2012_2_1
but what i need output is:
Jan312012 2012_1_31
Thanks
Upvotes: 21
Views: 45307
Reputation: 1
const today = new Date();
const date = new Date(Date.UTC(today.getFullYear(), today.getMonth(), 0, 0,0,0,0)).toISOString()
console.log(date)
Upvotes: -1
Reputation: 163
From the answers I was get most of them were failing to capture 31 in the May so I tried out several options and these are the ones I came up with. Hope, some may find this useful.
const today = new Date()
const lastMonth = today.getMonth() === 0 ? 11 : today.getMonth() - 1
const year = lastMonth === 0 ? today.getFullYear - 1 : today.getFullYear
const lastDayOfLastMonth = new Date(year, lastMonth + 1, 0).getDate()
const lastDateTimeOfLastMonth = new Date(year, lastMonth + 1, 1)
const lastDateOfLastMonth = new Date(year, lastMonth + 1, 0).toLocaleDateString()
console.log(lastDayOfLastMonth) // eg 31
console.log(lastDateTimeOfLastMonth) // eg 2021-05-31T16:00:00.000Z
console.log(lastDateOfLastMonth) // eg 31/05/2021
Upvotes: 1
Reputation: 1
try this
function dateLastMonth() {
var date = new Date();
date.setDate(0);
return date.getDate().toString() + '/' + date.getMonth().toString() + '/' + date.getFullYear().toString();
}
Upvotes: 0
Reputation: 57640
var d=new Date(); // current date
d.setDate(1); // going to 1st of the month
d.setHours(-1); // going to last hour before this date even started.
now d
contains the last date of the previous month. d.getMonth()
and d.getDate()
will reflect it.
This should work on any date api that wraps c time.h
.
Upvotes: 55
Reputation: 1
const monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
let today = new Date();
let dd = today.getDate();
let mm = today.getMonth()+1;
let yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
dd = new Date(yyyy, mm-1, 0).getDate();
let month = '';
mm == 1 ? month = monthNames[monthNames.length - 1] : month = monthNames[mm-2];
mm == 1 ? yyyy = yyyy - 1 : '';
let lastDay = month+'-'+dd+'-'+yyyy;
Upvotes: -3
Reputation: 19
var lastdayoflastmonth = new Date();
lastdayoflastmonth.setMonth(lastdayoflastmonth.getMonth(), 0);
var firstdayoflastmonth = new Date();
firstdayoflastmonth.setDate(1);
firstdayoflastmonth.setMonth(firstdayoflastmonth.getMonth()-1);
Upvotes: 1
Reputation: 900
This works perfectly for me:
var date = new Date();
date.setDate(0);
date now contains the last day of the previous month.
Upvotes: 36
Reputation: 413682
Just subtract one from the day-of-month:
var today = new Date();
var yesterday = new Date().setDate(today.getDate() - 1);
If the "date" property (the day-of-month) is 1, then setting it to zero will give you a date representing the last day of the previous month. Your code was pretty close already; you could just construct a new Date instance:
if (d.getDate() === 1)
curr_date = new Date().setDate(0).getDate();
In fact you don't even need an "if" statement:
curr_date = new Date().setDate( d.getDate() - 1 ).getDate();
and that'll work for any day of the month.
Upvotes: 0