How to get previous date in javascript

好久不见. 提交于 2020-06-16 22:33:45

问题


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?


回答1:


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




回答2:


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




回答3:


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>



回答4:


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/



来源:https://stackoverflow.com/questions/62264996/how-to-get-previous-date-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!