Get the first and last moment of a day in Javascript

Search for a command to run...

No comments yet. Be the first to comment.
Using typeof To check what type is a variable you would most likely use the typeof function. You can even skip the parenthesis (typeof someVariable is same as typeof(someVariable)). typeof works great for: strings numbers (not if checking integer vs...

Obviously your best guess is to ask the user. That being said, we generally want to avoid interrupting the user. Websites and apps are (or at least should be) about what the user's needs not ours. That being said, given the many legislation requirem...

Skip to solution If you have different language settings in your website or application wouldn't it be great if you could automatically set the language the user prefers? Doing so may be easer than you think. All you have to do is use navigator.langu...

Skip to solution. Problem Knowing your customer location can help you tailor their experience. Other times you may want to check for this information to compile statistics or comply with regulations. What's the best way to get location data? One way...

Here are 2 ready-to-use functions to get the first and last millisecond of a day using a few lines of vanilla JavaScript.
/**
* Get first millisecond of a date
* @param {Date} dateObj - JS Date object, default is current time
* @returns date object with time set to first millisecond of the day
*/
const beginningOfDay = function beginningOfDay (dateObj) {
//Get current date
const now = dateObj || new Date();
const year = now.getFullYear();
const month = now.getMonth();
const day = now.getDate();
return new Date(year, month, day);
};
In case of no argument we set the date to current date and time. The below basically means if dateObj is defined set now to that, otherwise set it to a new Date object.
const now = dateObj || new Date();
new Date() returns the current date and time. Similarly, any Date object you input as an argument will probably also have a time. To get the start of a day we will need a fresh start. For that we first extract the year, month and day.
const now = dateObj || new Date();
const year = now.getFullYear();
const month = now.getMonth();
const day = now.getDate();
By generating a new Date using year, month and day as arguments you set the date of the JavaScript Date object. Because you didn't set the time, JS will set it to the first value which is 0 (0 hours, 0 minutes, 0 seconds, 0 milliseconds) .
return new Date(year, month, day);
/**
* Get last millisecond of a date
* @param {Date} dateObj - JS Date object, default is current time
* @returns date object with time set to last millisecond of the day
*/
const lastMomentOfDay = function lastMomentOfDay(dateObj) {
//Tomorrow = Current day + 1
let tomorrow = beginningOfDay(dateObj);
tomorrow.setDate(tomorrow.getDate() + 1);
//Tommorow - 1 milisecond = result
let result = new Date();
result.setTime(tomorrow.getTime() - 1);
return result;
};
We leverage the function we wrote above (have a 🍪 for keeping your code DRY).
let tomorrow = beginningOfDay(dateObj);
tomorrow.setDate(tomorrow.getDate() + 1);
Remember, we got the first moment of tomorrow. If we subtract a millisecond from that, we will get the last millisecond the the day before, which is exactly what we are looking for.