Get previous month date javascript [duplicate] - javascript

This question already has answers here:
How to get 30 days prior to current date?
(16 answers)
Closed 7 years ago.
How can I get previous month date in javascript. Suppose you have today's date like:
var abc = new date();
It will return today's date for example 03-11-2015. Now I want to get 03-10-2015. This is 30 days less than todays date. How can I do this?

var d = new Date();
d.setMonth(d.getMonth() - 1);
Check out momentjs, great little library for manipulating and formatting dates.

Complementing Robert Shenton's answer:
var d = new Date();
var newMonth = d.getMonth() - 1;
if(newMonth < 0){
newMonth += 12;
d.setYear(d.getFullYear() - 1); // use getFullYear instead of getYear !
}
d.setMonth(newMonth);

Related

Get date of exactly 6 days ago from today in JS [duplicate]

This question already has answers here:
How to subtract days from a plain Date?
(36 answers)
Closed 3 years ago.
Thank you in advance
I would like your help with getting 'days ago' from a particular date. I don't want to use any library.
Although I have tried moment JS.
Use getDate() and subtract the number of days from it
var d = new Date();
d.setDate(d.getDate() - 6);
console.log(d);
First, make a new Date with your date:
const date = new Date('December 17, 1995 03:24:00');
Second, subtract 6 days like so:
date.setDate(date.getDate() - 6);
Third, use date.toString() :
console.log(date.toString());
You question title and description contradict with each other.
The following function that return number of days ago can help if this is what you need:
function getDaysAgo(date, now = new Date()) {
//first calculating start of the day
const start = now.setHours(0, 0, 0, 0);
//then calculating difference in miliseconds
const diff = start - date.getTime();
//finally rounding to a bigger whole days
const result = Math.ceil(diff/(1000*60*60*24));
//as a bonus returning today/yesterday/future when necessary
if (result < 0) {
return 'in future';
}
if (result === 0) {
return 'today';
}
return result === 1 ? 'yesterday' : result + ' days ago';
}
For example getDaysAgo(new Date(Date.parse('2019-9-28 23:59')), new Date(Date.parse('2019-9-30 10:59'))) returns 2 days ago.
It is a simple function that returns a new desire past date.
function getNthDate(nthDate){
let date = new Date();
return new Date(date.setDate(date.getDate() - nthDate))
}
Live example

JavaScript struggling with date format [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 5 years ago.
I am trying to retrieve a date but I am struggling with the month because January = 0 instead of 1. I need to retrieve items which occured after today -60 days.
var today = new Date();
if (today.getMonth() < 1) {
var numberOfDaysLastMonth = getDaysInMonth(12,today.getFullYear()-1); //number of days in december last year
var numberOfDaysThisMonth = getDaysInMonth(today.getMonth()+1,today.getFullYear()); //number of days this month
} else {
var numberOfDaysLastMonth = getDaysInMonth(today.getMonth(), today.getFullYear()); //number of days last month
var numberOfDaysThisMonth = getDaysInMonth(today.getMonth()+1, today.getFullYear()); //number of days this month
};
var startDate = new Date();
var myMonth;
var myYear;
if (today.getMonth() < 1) {
myMonth = today.getMonth() + 1;
myYear = today.getFullYear()-1;
} else {
myMonth = startDate.getMonth()-1;
myYear = today.getFullYear();
};
startDate = myYear+"-"+myMonth+"-"+startDate.getDate(); // returns last month
Is there a more simple (and working) way to do this?
If it's difficult to use dates as they are in Javascript, then modify them to work as they want them to work. An example:
Date.prototype.getMyMonth = function() {return this.getMonth() + 1;};
Now, you can change your code to use .getMyMonth instead of .getMonth.

Determine start and end time of current day (UTC) (JAVASCRIPT) [duplicate]

This question already has answers here:
How to get start and end of day in Javascript?
(13 answers)
Closed 7 years ago.
I am trying to determine start and end time of the current day in UTC.
Currently this is my solution. `
var starttime = date.getTime() -(date.getHours() * 3600000 ); //subtracting a hours to go back to start time of the day.
var endtime = low + 86400000; //adding 24 hours to the start time
`
Any help is appreciated ! Thank you
Thank you everyone. This is what worked for me !! :)
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var startHour =Date.UTC(year,month,day,0,0,0,0);
var endHour = startHour + 86400000;

Get last day of the month from '2015-02-23' string (javascript) [duplicate]

This question already has answers here:
Get number days in a specified month using JavaScript? [duplicate]
(4 answers)
Closed 8 years ago.
I have a string that represents a date in this format: 2015-02-23
I need to use this date to get the last day of the month.
How should I do the necessary conversions to achieve that?
Here's a function that should work for you:
function getLastDayInMonth(s) {
var date = new Date(s);
var lastDate = date;
var month = date.getMonth();
while (date.getMonth() == month) {
lastDate = date;
date = new Date(lastDate.getTime() + 1000 * 60 * 60 * 24); //add 1 day
}
return lastDate.toDateString();
}
var lastDay = getLastDayInMonth('2015-02-23');
alert(lastDay);

Calculate javascript date [duplicate]

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
i have the date 2013-12-28 and i want add one or more more day to it. so if i add one more day it will be 2013-12-29.
i try to add it by adding the value of it's date (date 28+1), it works, but what if i add 7 more day to it? the date will be 35, and of course it is not a valid date format.
can someone help me?
here's the example of my script:
var d = new Date();
var Y = d.getFullYear();
var M = d.getMonth()+1;
var D = d.getDate();
var DT = d.getDate()+1;// what if i + 20 days from today? the format would be invalid
var today = Y+"-"+M+"-"+D;
var tomorrow = Y+"-"+M+"-"+DT;
alert(today+" <> "+tomorrow);
// "<>" means nothing
You may try like this using getdate(), setdate() and getdate():
var myDate = new Date();
myDate.setDate(myDate.getDate() + 7);
If you already have a date object as in the code you show:
var d = new Date();
...then you can add 7 days to it like this:
d.setDate( d.getDate() + 7 );
...and it will automatically increment the month if needed.
Further reading:
The Date object
.getDate() method
.setDate() method
If you need to extract the year, month and day in order to format the result a particular way do so after adding days.
The solution is to convert your date string into unix timestamp, and them add 3600 * 24 * <number of days> to the timestamp and them convert it back to date string.
The code can be as follows:
function addDaysToDate(date, days) {
var time = Date.parse(date) + days * 24 * 3600;
date = new Date(time);
return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
}
var date = '2013-12-28';
console.log(addDaysToDate(date, 7));

Categories