jquery - Getting Previous Date Using Javascript - Stack Overflow

I want to get the before six months date using javascript. I am using the following method.var curr = d

I want to get the before six months date using javascript.

I am using the following method.

var curr = date.getTime(); // i will get current date in milli seconds
var prev_six_months_date = curr - (6* 30 * 24 * 60* 60*1000);
var d = new Date();
d.setTime(prev_six_months_date);

Is this the right way or any better way to get the last six months date.

If this get fixed I want to apply this logic to get previous dates like last 2 months and last 10 years etc.

If any body give the solution in jquery also very helpful to me. Thanks in advance.

I want to get the before six months date using javascript.

I am using the following method.

var curr = date.getTime(); // i will get current date in milli seconds
var prev_six_months_date = curr - (6* 30 * 24 * 60* 60*1000);
var d = new Date();
d.setTime(prev_six_months_date);

Is this the right way or any better way to get the last six months date.

If this get fixed I want to apply this logic to get previous dates like last 2 months and last 10 years etc.

If any body give the solution in jquery also very helpful to me. Thanks in advance.

Share Improve this question edited Feb 8, 2012 at 12:06 jabclab 15.1k5 gold badges57 silver badges51 bronze badges asked Feb 8, 2012 at 12:03 user1049997user1049997 1,5934 gold badges14 silver badges18 bronze badges 2
  • Since you tagged it jQuery, you can replace date.getTime() with $.now(). – alex Commented Feb 8, 2012 at 12:12
  • It has been a long time since this question, but still : var d = new Date(); d.setMonth(d.getMonth()-6) Similarly, you can use setYears() and setDate() – Scrooge McD Commented Mar 22, 2015 at 18:38
Add a ment  | 

3 Answers 3

Reset to default 6

Add more functionality to the Date

Date.prototype.addDays = function (n) {
    var time = this.getTime();
    var changedDate = new Date(time + (n * 24 * 60 * 60 * 1000));
    this.setTime(changedDate.getTime());
    return this;
};

Usage

var date = new Date();
/* get month back */
date.addDays(-30);

/* get half a year back */
date.addDays(-30 * 6);

No need for extra libraries, if this is only thing you need regarding dates. You can also create more functions to the Date's prototype according to your needs.

I would look into date.js. It's well tested and has a very fluent interface for manipulating dates and times in JavaScript.

An example of using date.js:

(6).months().ago()

Try:

var curr = new Date();
var prev_six_months_date = new Date(curr);
var prev_two_months_date = new Date(curr);
var prev_ten_years_date = new Date(curr);
prev_six_months_date.setMonth(curr.getMonth() - 6);
prev_two_months_date.setMonth(curr.getMonth() - 2);
prev_ten_years_date.setFullYear(curr.getFullYear() - 10);
console.log(prev_six_months_date.toString());
console.log(prev_two_months_date.toString());
console.log(prev_ten_years_date.toString());
console.log(curr.toString());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743734111a4497940.html

相关推荐

  • jquery - Getting Previous Date Using Javascript - Stack Overflow

    I want to get the before six months date using javascript. I am using the following method.var curr = d

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信