4

Why does a call to Javascript's Date library such as date.setMonth(date.getMonth()-1) return 1426456040720 here?

I expect you may get different numbers due to the timezone and culture.

I feel like I must be missing something, I'm sure this is how I've always decremented a month.

Here is a codepen:

http://codepen.io/hally9k/pen/pvMxBP?editors=101

var thisMonth = new Date();

console.log('this month: ' + thisMonth);

var lastMonth = thisMonth.setMonth(thisMonth.getMonth() - 1);

console.log('last month: ' + lastMonth); 
hally9k
  • 2,423
  • 2
  • 25
  • 47
  • `Date.setMonth()` gives you the new date string in a unix timestamp. `new Date()` will use the `Date.prototype.toString()` method, which gives you a timestamp in the `Date.toString()` format. – Jared Farrish Apr 15 '15 at 23:00

5 Answers5

5

The return value from .setMonth() is the ms since epoch of the date object. You can see that intended return value in the ECMAScript specification here.

.setMonth() modifies its Date object. You can obtain the new month value from that object with .getMonth() if you want the new month value.

If you just want the new month after you've changed it, you can do this:

var d = new Date();
console.log('this month: ' + d.getMonth());
d.setMonth(d.getMonth() - 1);
console.log('previous month: ' + d.getMonth()); 
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • This doesn't work where *d* is on a date that doesn't exist in the previous month, which happens on 7 dates for a non–leap year. E.g. on 31 December, subtracting one month gives 31 November, which rolls over to 1 December, so "this month" and "previous month" are both 11. – RobG Nov 12 '19 at 11:40
  • @RobG - What are you expecting to happen when you provide a non-existing date? It does not seem unusual that the behavior is undefined (not necessarily what one might expect). – jfriend00 Nov 12 '19 at 18:12
1

thisMonth.setMonth sets the month to thisMonth and return the new date with milli seconds.

so you have 2 choices.

  1. to print thisMonth
  2. print new Date(lastMonth)
Omar Elawady
  • 3,300
  • 1
  • 13
  • 17
  • Is it the new date IN milliseconds? Hence passing it into the constructor for date can instantiate the correct date... ? – hally9k Apr 15 '15 at 23:03
1

Construct a new Date:

var lastMonth = new Date(thisMonth.setMonth(thisMonth.getMonth() - 1));

setMonth() returns a number that represents milliseconds since 01 January, 1970 UTC. Using new Date will transform that number in a Date format.

You can use lastMonth.getMonth() to get the month, but if you want it for display purposes take in consideration that getMonth() returns from 0 to 11. (0 is January).

Mihai Alex
  • 678
  • 6
  • 13
1

The issue may be related to the fact that this solution could contain errors in edge cases, such as the 31st day of a month or leap years. As per this StackOverflow question, you would want your code from the CodePen to look something like this:

var thisMonth = new Date();

$('#date').html(thisMonth);

var currentMonth = thisMonth.getMonth();
thisMonth.setMonth(thisMonth.getMonth() - 1);

if (thisMonth.getMonth() != ((currentMonth - 1) % 12)) {
  thisMonth.setDate(0);
}

$('#prevDate').html(thisMonth); 
Community
  • 1
  • 1
Donald
  • 1,120
  • 12
  • 21
0

DateTime.setMonth returns the unix timestamp of the month being set and changes the value of the DateTime object it is called on.

E.g. if you change the last line to this it will be the number 1:

console.log('last month: ' + thisMonth.getMonth()); 
James G.
  • 2,852
  • 3
  • 28
  • 52