408

As title says, I'm stuck on finding a way to get the first and last date of the current month with JavaScript or jQuery, and format it as:

For example, for November it should be :

var firstdate = '11/01/2012';
var lastdate = '11/30/2012';
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Moozy
  • 4,735
  • 3
  • 18
  • 18
  • 5
    Don't make it difficult, last day of a month is -1 day of the 1st day of the following month. This was nice: http://stackoverflow.com/a/1924846/168703 – JonH Nov 26 '12 at 19:28

2 Answers2

1095

Very simple, no library required:

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

or you might prefer:

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

EDIT

Some browsers will treat two digit years as being in the 20th century, so that:

new Date(14, 0, 1);

gives 1 January, 1914. To avoid that, create a Date then set its values using setFullYear:

var date = new Date();
date.setFullYear(14, 0, 1); // 1 January, 14
RobG
  • 142,382
  • 31
  • 172
  • 209
  • 2
    tried with code in accepted answer, I get firstDay as "Fri Aug 01 2014 00:00:00 GMT+0800 (SGT)" and lastDay as "Sun Aug 31 2014 00:00:00 GMT+0800 (SGT)" for the current month which is not correct answer. Has something changed ? – Kush Aug 26 '14 at 08:32
  • 1
    @Kush what answer are you expecting exactly? – user3281466 Aug 26 '14 at 10:12
  • 5
    as asked in question by @moozy, i am looking for same: var firstdate = '11/01/2012'; var lastdate = '11/30/2012'; – Kush Aug 26 '14 at 10:40
  • 1
    @Kush—the current month is August, so it will return dates for August. If you want some other month, create a date for the first of the month and use the above algorithm to get the last, e.g. for November 2014: `var firstDay = new Date(2014,10,1); var lastDay = new Date(2014,11,0);`. – RobG Aug 26 '14 at 12:06
  • @RobG I understand the current month is August so I will get dates of August. But the question was to get a formatted date like "11/01/2012" in javascript/jquery and the code in accepted answer returns a different format which I mentioned earlier ("Sun Aug 31 2014 00:00:00 GMT+0800 (SGT)"). – Kush Aug 26 '14 at 12:27
  • 1
    @kush—the code in the answer returns a Date object. If you send that to output using, say, *console.log* or *window.alert*, then the built–in [*Date.prototype.toString*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.2) method will be called to display the date in a human readable form. The result of that is entirely implementation dependent and has nothing to do with this answer. If you want to present a date in a particular format, there are [*lots of questions*](http://stackoverflow.com/search?q=%5Bjavascript%5Dformat+a+date) about that here on SO. :-) – RobG Aug 26 '14 at 23:10
  • How can we get same day of each month? – Superman Jan 11 '15 at 01:59
  • @Superman—I don't understand your comment. If you have a question about that, post it as a question with a full explanation and perhaps some code so you'll get a full answer. ;-) – RobG Jan 13 '15 at 11:23
  • 9
    For those like me who wondered about the use of "December+1" and day "0" of the month to get the last day of the previous month, it all works per the spec; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Ed Staub Oct 24 '15 at 03:31
  • 1
    U can use this simple method https://stackoverflow.com/a/48013274/5078763 – Sujay U N Dec 28 '17 at 19:32
  • @SujayUN—the OP wants a formatted date string or Date object, your linked answer gives just the day number and uses the same algorithm as most of the answers here. – RobG Dec 31 '17 at 22:09
  • This creates dates with wrong GMT, so the values retrieved are wrong in some cases.. (Eg: Fri Aug 01 2014 00:00:00 GMT+0800 (SGT) should be Fri Aug 01 2014 08:00:00 GMT+0800 (SGT)) You'd better use the setUTCDate() method to retrieve the first day then create the last day from this, the GMT of your current date will be conserved – Tofandel Sep 17 '19 at 21:06
  • @Tofandel—by "wrong GMT" do you mean offset? That's impossible as Dates don't have an offset. The first example may produce an incorrect result if run in a very narrow timespan on the day that the local daylight saving offset change occurs (noting that Singapre doesn't observe daylight saving). The second example avoids that. The question is about getting a date, the time is irrelevant. You might set the time to 00:00:00.000 in the first example, the second does that by default. – RobG Sep 18 '19 at 05:16
  • The time is actually relevant, the problem is when you get the date with the wrong offset, if you do date.toJSON().substr(0,10) you will sometimes get the date of the next or previous day because the JSON string representation of a string is UTC and what you got is not – Tofandel Sep 18 '19 at 07:36
  • @RobG Sorry, my mistake.. – Naisarg Parmar Jan 17 '20 at 07:48
  • 2
    love the simplicity! – Kitson Feb 17 '20 at 22:00
  • Today is `03 Oct 2020`, with the `new Date(date.getFullYear(), date.getMonth(), 1)`, I get `30 Sep 2020`. I thought it should be `01 Oct 2020`? – Antonio Ooi Oct 03 '20 at 10:46
  • Ok, sorry, it's correct when using `toLocaleDateString()`, thanks! – Antonio Ooi Oct 03 '20 at 10:58
  • @Tofandel—typically dates ignore timezones, they are just dates. If the intention is to create a string date for JSON without a time or timezone, then do not use the default *JSON.stringify* method, use a [replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) that creates an appropriate date–only string. – RobG Oct 05 '20 at 21:44
  • this code can worked on mac too? – fahdshaykh Aug 24 '22 at 10:43
  • 1
    I would highly recommend to create a UTC date before creating the start and end date with the Date constructor. Otherwise the code does not work in summer / winter time in case the system clock is using it. Hence the answer should look like this: `var startDate = new Date(Date.UTC(year, month, 1)); var endDate = new Date(Date.UTC(year, month + 1, 0));` – beckersense Oct 05 '22 at 15:19
  • @beckersense—daylight saving has no effect at all on the code in the answer. The same general algorithm applies to all offsets and timezones, UTC is not a special case. – RobG Oct 06 '22 at 00:04
23

I fixed it with Datejs

This is alerting the first day:

var fd = Date.today().clearTime().moveToFirstDayOfMonth();
var firstday = fd.toString("MM/dd/yyyy");
alert(firstday);

This is for the last day:

var ld = Date.today().clearTime().moveToLastDayOfMonth();
var lastday = ld.toString("MM/dd/yyyy");
alert(lastday);
ksav
  • 20,015
  • 6
  • 46
  • 66
Moozy
  • 4,735
  • 3
  • 18
  • 18