[英]How to get three month ago date in javascript in the format as YYYYDDMM efficiently
我知道在java中的一種方法:
Calendar c5 = Calendar.getInstance();
c5.add(Calendar.MONTH, -6);
c5.getTime(); //It will give YYYYMMDD format three months ago.
有沒有辦法在javascript中執行此操作。 我知道我可以使用Date d = new Date(); 解析它並做一些代碼來獲取格式。 但現在我不想做解析並獲得三個月前的約會。
var dt = new Date('13 June 2013');
dt.setMonth(dt.getMonth()-1)
然后,您可以使用此答案中的這段代碼將其轉換為YYYYMMDD
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); // padding
};
d = new Date();
d.yyyymmdd();
需要注意的事情。 如果你在3月31日減去一個月,會發生什么? 你不能得到2月31日! 有關詳細信息,請參閱此答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.