简体   繁体   中英

How to get previous date in javascript

I have one date but i want to get previous date from that.How we can do using javascript or momentjs?

Demo: https://stackblitz.com/edit/js-ykswiz?file=index.js

date format dd-mm-yyyy.

Example:

 var date1=new Date("08-06-2020");
 console.log("Prev Date="+date1.getDate() - 1);// Prev Date=07-06-2020


 var date2=new Date("01-06-2020");
 console.log("Prev Date="+date2.getDate() - 1);// Prev Date=31-05-2020


 var date3=new Date("01-01-2021");
 console.log("Prev Date="+date3.getDate() - 1);// Prev Date=31-12-2020

Is it possible in javascript?

first, if you pass a string to the new Date() function, it should be in this format new Date('MM-dd-yyyy')

so the first 2 digits represent the month

the second 2 digits represent the date

the next 4 digits represent the year

so new Date("08-06-2020") means August 06, 2020 not June 08, 2020

to get the previous day of some date, we can use the moment to subtract 1 day like the following

var date1 = new Date('08-06-2020'); // Aug 06, 2020
var date11 = moment(date1).subtract(1, 'days').format('DD-MM-YYYY');

console.log(`date1 >> ${date1}`);
console.log(`date11 >> ${date11}`);


var date2 = new Date('01-06-2020'); // Jan 06, 2020
var date22 = moment(date2).subtract(1, 'days').format('DD-MM-YYYY');

console.log(`date2 >> ${date2}`);
console.log(`date22 >> ${date22}`);

hope it helps

Since the issue with the Date constructor is explained and solved by one of the above answers, this solution is for the ones who use moment() instead of new Date()

var date = moment('08-06-2020', 'DD-MM-yyyy');

var previousDate = moment(date).subtract(1, 'days');  //derive the previous date
var nextDate = moment(date).add(1, 'days');   //derive the next date

console.log(previousDate.format('DD-MM-yyyy'));  //output => 07-06-2020
console.log(nextDate.format('DD-MM-yyyy'));  //output => 09-06-2020

read more about adding and subtracting dates in moment.js

https://momentjs.com/docs/#/manipulating/subtract/

https://momentjs.com/docs/#/manipulating/add/

read more about formatting dates in moment.js

https://momentjs.com/docs/#/parsing/string-format/

you can use the setDate and getDate functions;

var date1=new Date("08-06-2020");
date1.setDate(date1.getDate() - 1);
console.log("Prev Date="+date1.getDate());// Prev Date=08-05-2020

hope its what you asked for. btw, in this format tha previous date will be 08-05-2020 since the default is mm-dd-yyyy

You could achieve this using momentjs . Use the moment constructor to parse the input string then use the subtract() to subtract the specified number of days from the moment object. Resultant can be formatted according to your requirements

 let arr = ["08-06-2020", "01-06-2020", "01-01-2021"]; let prevDates = arr.map(item => { let momentItem = moment(item, "DD-MM-YYYY"); let prevDay = momentItem.subtract(1, "days"); return prevDay.format("DD-MM-YYYY"); }); console.log(prevDates);
 <script src="https://momentjs.com/downloads/moment.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM