What is the simplest way to obtain an instance of new Date()
but set the time at midnight?
11 Answers
The setHours
method can take optional minutes
, seconds
and ms
arguments, for example:
var d = new Date();
d.setHours(0,0,0,0);
That will set the time to 00:00:00.000
of your current timezone, if you want to work in UTC time, you can use the setUTCHours
method.

- 12,119
- 5
- 32
- 34

- 807,428
- 183
- 922
- 838
-
66FYI, I'm running this method ( d.setHours(0,0,0,0) ) in a reduce function on 270K rows, and it's over 20 seconds faster than doing d.setHours(0); d.setMinutes(0); d.setSeconds(0); Great answer @CMS! – jbnunn May 09 '12 at 20:05
-
55This needs to be done in two lines to keep d as a Date object. I recently fixed a bug that did it in one line: var d = new Date().setHours(0, 0, 0, 0). However, d was then a number, not a date since setHours returns a number. – Jason Apr 21 '14 at 20:43
-
23I've just come across this (late to the party I know), also needing a one line solution returning a Date. For anyone looking for this, [@Zon's answer](https://stackoverflow.com/questions/3894048/what-is-the-best-way-to-initialize-a-javascript-date-to-midnight#answer-30100627) works perfectly: `new Date(new Date().setHours(0,0,0,0))`. – SRack Nov 01 '17 at 14:58
-
Quirk: If you enter today's date as a string it will somehow set the date to yesterday... This is how to get today's date with a string input: `new Date(new Date('2021-04-29').setHours(24,0,0,0)).getDate(); // get 29` – John Vandivier Apr 29 '21 at 19:07
-
`new Date(new Date(element).setHours(0,0,0,0));` works perfect for me – Gloria Chen Feb 28 '22 at 04:06
Just wanted to clarify that the snippet from accepted answer gives the nearest midnight in the past:
var d = new Date();
d.setHours(0,0,0,0); // last midnight
If you want to get the nearest midnight in future, use the following code:
var d = new Date();
d.setHours(24,0,0,0); // next midnight
-
9What happens if today is 25 hours long (clocks adjust forward for Daylight Saving)? – qntm Sep 06 '13 at 16:19
-
9This is client side script, midnight is midnight, despite daylight savings.. Also noteworthy mention.. not every place in the world uses DST (daylight savings time) – chris Nov 21 '13 at 19:40
-
2@qntm: daylight saving time change is always applied at 2am-4am, so that it will be only one midnight during that day :). But yes, 3am can happen twice during the day (facepalm) – Dan Nov 22 '13 at 09:39
-
@chris No, I mean the second snippet. "24 hours after midnight today" is not always the same thing as "midnight tomorrow". In these cases, does the second snippet work? – qntm Nov 23 '13 at 17:18
-
5@qntm: Date.setHours does not blindly add 24 hours, it's more intelligent. Instead, it returns a timestamp of the moment, when the clock will show 24 next time. Taking into consideration time saving. Try to play in the console yourself. Interesting, setHours(25) returns the timestamp of the tomorrows 1am – Dan Nov 25 '13 at 13:54
-
@Dan Is this inclusive? Say, it is currently exactly midnight, 0 seconds, miliseconds, etc. Will this just keep the same date, or will it set the date object back to the previous day? (If it is exclusive, then checking the hours for this edge case, before setting it would be appropriate).Tried searching for more info online, but couldn't find it, so I thought I'd ask you. – GangstaGraham May 14 '15 at 02:57
-
@GangstaGraham, the first snippet `setHours(0,0,0,0)` is inclusive, while the second `setHours(24,0,0,0)` is exclusive, setting the date to tomorrow – Dan May 14 '15 at 14:21
A one-liner for object configs:
new Date(new Date().setHours(0,0,0,0));
When creating an element:
dateFieldConfig = {
name: "mydate",
value: new Date(new Date().setHours(0, 0, 0, 0)),
}

- 18,610
- 7
- 91
- 99
-
5Please be aware that this date is related to your timezone! Check your date with `toISOString()` if it matches your timezone criteria. Safest ist to use `setUTCHours`, if you want to make it compatible with zero UTC offset. – Benny Code Nov 30 '20 at 17:42
Just going to add this here because I landed on this page looking for how to do this in moment.js and others may do too.
[Rationale: the word "moment" already appears elsewhere on this page so search engines direct here, and moment.js is widespread enough to warrant to being covered going on how often it is mentioned in other date-related SO questions]
So, in version 2.0.0 and above:
date.startOf('day');
For earlier versions:
date.sod();
Docs:

- 14,137
- 7
- 49
- 51
-
1to get a date object back, use this `moment(DATE_OBJECT).startOf('day').toDate();` – xinthose Jan 29 '16 at 17:37
-
23And what if you're already using moment.js for one of the many good reasons you might...? How about reading the rationale I gave before adding a sarcastic comment? – andyhasit Mar 31 '16 at 09:20
You can probably use
new Date().setUTCHours(0,0,0,0)
if you need the value only once.

- 277
- 3
- 3
-
3
-
2This is deffinitely best answer, but needs a little edit to return an instance of a Date. new Date(new Date().setUTCHours(0, 0, 0, 0)); – VitFL Apr 20 '20 at 21:16
If calculating with dates summertime will cause often 1 hour more or one hour less than midnight (CEST). This causes 1 day difference when dates return. So the dates have to round to the nearest midnight. So the code will be (thanks to jamisOn):
var d = new Date();
if(d.getHours() < 12) {
d.setHours(0,0,0,0); // previous midnight day
} else {
d.setHours(24,0,0,0); // next midnight day
}
Adding usefulness to @Dan's example, I had the need to find the next midday or midnight.
var d = new Date();
if(d.getHours() < 12) {
d.setHours(12,0,0,0); // next midnight/midday is midday
} else {
d.setHours(24,0,0,0); // next midnight/midday is midnight
}
This allowed me to set a frequency cap for an event, only allowing it to happen once in the morning and once in the afternoon for any visitor to my site. The date captured was used to set the expiration of the cookie.

- 3,610
- 8
- 34
- 50
I have made a couple prototypes to handle this for me.
// This is a safety check to make sure the prototype is not already defined.
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
return this;
}
};
Date.method('endOfDay', function () {
var date = new Date(this);
date.setHours(23, 59, 59, 999);
return date;
});
Date.method('startOfDay', function () {
var date = new Date(this);
date.setHours(0, 0, 0, 0);
return date;
});
if you dont want the saftey check, then you can just use
Date.prototype.startOfDay = function(){
/*Method body here*/
};
Example usage:
var date = new Date($.now()); // $.now() requires jQuery
console.log('startOfDay: ' + date.startOfDay());
console.log('endOfDay: ' + date.endOfDay());

- 1,525
- 1
- 14
- 11
In case you already have d3.js as a dependency in your project, or don't mind bringing it in, d3-time (d3.js library is modular as of v4.0.0) has got Intervals.
They might prove useful when setting dates to "default" values, e.g. midnight, 0.00 seconds, the first of the month, etc.
var d = new Date(); // Wed Aug 02 2017 15:01:07 GMT+0200 (CEST)
d3.timeHour(d) // Wed Aug 02 2017 00:00:00 GMT+0200 (CEST)
d3.timeMonth(d) // Tue Aug 01 2017 00:00:00 GMT+0200 (CEST)

- 161
- 1
- 1
- 9
-
2So I've noticed a trend in JavaScript related questions posted on SO: no matter the complexity of the solutions put forward using native JS, any answers addressing the issue with the help of an external library will inevitably at some point in time get downvoted. Realizing this, I took care to post my answer with the quick preface: _In case you already have example.js as a dependency_. And yet still the downvote, anonymous SO user? that hurts, man :'- ( – Peemster Jan 10 '18 at 14:57
-
I fell that this answer should not be down voted especially since the one proposing usage of moment.js has 16 up votes (at the moment of writing this comment). It does solve the problem in a very concise and not that obvious way. – luke-at-work Feb 26 '18 at 10:43
Using the dayjs library, you can use the startOf('day') method.
const dayjs = require('dayjs');
const todayAtMidnight= dayjs().startOf('day');
// Get as a native date object
console.log(todayAtMidnight.toDate());
To get the start of a particular day you can use the following:
const date = dayjs("2023-02-12").startOf('day');

- 449
- 5
- 14
I have a DateUtil class in my project that I added some functions to it, btw I wanna share with you those which is related to this topic
export class DateUtil {
static getCurrentUTCDate(): Date {
return new Date(Date.now() + new Date().getTimezoneOffset() * 60 * 1000); // Convert to UTC timezone
}
static getUTCStartOfDay(): Date {
const now = DateUtil.getCurrentUTCDate();
return new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
0,
0,
0,
0
)
);
}
static getUTCEndOfDay(): Date {
const now = DateUtil.getCurrentUTCDate();
return new Date(
Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
23,
59,
59,
999
)
);
}
}
Usage example
const startOfDayISOString = DateUtil.getUTCStartOfDay().toISOString(); // 2023-04-24T00:00:00.000Z
const endOfDayISOString = DateUtil.getUTCEndOfDay().toISOString(); // 2023-04-24T23:59:59.999Z

- 761
- 7
- 18