Javascript date last day

Javascript date last day

Last updated: Jan 14, 2023
Reading time · 3 min

banner

# Get the First and Last Day of a Year in JavaScript

Use the Date() constructor to get the first and last day of a year.

The Date() constructor takes the year, a zero-based value for the month and the day as parameters and returns a Date object.

Copied!
// ✅ Get the first and last day of the current year const currentYear = new Date().getFullYear(); console.log(currentYear); // 👉️ 2023 const firstDay = new Date(currentYear, 0, 1); console.log(firstDay); // 👉️ Sun Jan 01 2023 const lastDay = new Date(currentYear, 11, 31); console.log(lastDay); // 👉️ Sun Dec 31 2022

You can use this approach to get the first and last day of any year.

Here is a reusable function that takes the year as a parameter and returns the last day of the year.

Copied!
function getLastDayOfYear(year) return new Date(year, 11, 31); > // 👇️ ✅ Get the last day of the current year const currentYear = new Date().getFullYear(); console.log(getLastDayOfYear(currentYear)); // 👉️ Sun Dec 31 2023 // 👇️ ✅ Get the last day of any year console.log(getLastDayOfYear(2025)); // 👉️ Wed Dec 31 2025

And here is a function that takes the year as a parameter and returns the first day of the year.

Copied!
function getFirstDayOfYear(year) return new Date(year, 0, 1); > // 👇️ ✅ Get the first day of the current year const currentYear = new Date().getFullYear(); console.log(getFirstDayOfYear(currentYear)); // 👉️ Sun Jan 01 2023 // 👇️ ✅ Get the first day of any year console.log(getFirstDayOfYear(2025)); // 👉️ Wed Jan 01 2025

We passed the following 3 arguments to the Date():

We used the Date.getFullYear method to get the current year.

Copied!
console.log(new Date().getFullYear()); // 👉️ 2023

The value of the month is zero-based, where January is 0 , February is 1 , March is 2 , and so on, up to December, which is 11 .

To get the first day of the current year, we passed 0 (January) for the month and 1 for the day of the month to the Date() constructor.

Copied!
const currentYear = new Date().getFullYear(); console.log(currentYear); // 👉️ 2023 const firstDay = new Date(currentYear, 0, 1); console.log(firstDay); // 👉️ Sun Jan 01 2023

To get the last day of the current year, we passed 11 (December) for the month and 31 for the day of the month.

Copied!
const currentYear = new Date().getFullYear(); console.log(currentYear); // 👉️ 2023 const lastDay = new Date(currentYear, 11, 31); console.log(lastDay); // 👉️ Sun Dec 31 2022

We specified 31 for the day to get the last day of the year. A calendar year always starts on January 1st and ends on December 31st.

# Getting the First and Last day of any year

You can use this approach to get the first and last day of any year. All you have to do is change the first parameter in the call to the Date() constructor.

Copied!
const year = 2030; const firstDay = new Date(year, 0, 1); console.log(firstDay); // 👉️ Tue Jan 01 2030 const lastDay = new Date(year, 11, 31); console.log(lastDay); // 👉️ Tue Dec 31 2030

If you want to make your code more readable, store the values for the month in variables with appropriate naming.

Copied!
const currentYear = new Date().getFullYear(); const january = 0; const firstDay = new Date(currentYear, january, 1); console.log(firstDay); // 👉️ Sat Jan 01 2022 const december = 11; const lastDay = new Date(currentYear, december, 31); console.log(lastDay); // 👉️ Sat Dec 31 2022

Defining separate variables that store the zero-based value for the month makes our code a bit more readable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Javascript date last day

Last updated: Jan 13, 2023
Reading time · 3 min

banner

# Get First (Sunday) and Last Day (Saturday) of the current Week

To get the first and last days of the current week, use the setDate() method to change the day of the month of a date to the first and last days of the current week and pass the result to the Date() constructor.

Copied!
const today = new Date(); // ✅ Get the first day of the current week (Sunday) const firstDay = new Date(today.setDate(today.getDate() - today.getDay())); // ✅ Get the last day of the current week (Saturday) const lastDay = new Date(today.setDate(today.getDate() - today.getDay() + 6)); console.log(firstDay); // 👉️ Sunday August 7 2022 console.log(lastDay); // 👉️ Saturday August 13 2022

The code sample above considers Sunday to be the first day of the week and Saturday to be the last.

# Get the First (Monday) and Last Day (Sunday) of the current Week

If your use case needs Monday to be the first day and Sunday to be the last, use this code snippet instead.

Copied!
const today = new Date(); // ✅ Get the first day of the current week (Monday) function getFirstDayOfWeek(d) // 👇️ clone date object, so we don't mutate it const date = new Date(d); const day = date.getDay(); // 👉️ get day of week // 👇️ day of month - day of week (-6 if Sunday), otherwise +1 const diff = date.getDate() - day + (day === 0 ? -6 : 1); return new Date(date.setDate(diff)); > const firstDay = getFirstDayOfWeek(today); // ✅ Get the last day of the current week (Sunday) const lastDay = new Date(firstDay); lastDay.setDate(lastDay.getDate() + 6); console.log(firstDay); // 👉️ Monday August 8 2022 console.log(lastDay); // 👉️ Sunday August 14 2022

The Date.getDate() method returns an integer between 1 and 31 that represents the day of the month for the given Date .

The Date.getDay method returns an integer between 0 and 6 that represents the day of the week for the given date: 0 is Sunday, 1 is Monday, 2 is Tuesday, etc.

The day of the month for the first day of the week is equal to day of the month — day of the week .

If you consider Monday to be the first day of the week, add 1 to the result.

The day of the month for the last day of the week is equal to first day of the week + 6 .

The last step is to use the Date.setDate() method, which takes the day of the month as a parameter and changes the value on the specific date.

The setDate() method returns the number of milliseconds between January 1st, 1970 and the given date.

To get a new Date object that represents the first day of the week, we pass the timestamp to the Date() constructor.

We repeated the same process to create a Date object that stores the last day of the week.

If you consider the first day of the week to be Monday:

  1. Subtract the day of the week from the day of the month.
  2. If the day of the week is Sunday, subtract 6 to get Monday.
  3. If it is any other day, add 1 because the getDay method returns a zero-based value.
Copied!
const today = new Date(); // ✅ Get the first day of the current week (Monday) function getFirstDayOfWeek(d) // 👇️ clone date object, so we don't mutate it const date = new Date(d); const day = date.getDay(); // 👉️ get day of week // 👇️ day of month - day of week (-6 if Sunday), otherwise +1 const diff = date.getDate() - day + (day === 0 ? -6 : 1); return new Date(date.setDate(diff)); > const firstDay = getFirstDayOfWeek(today); // ✅ Get the last day of the current week (Sunday) const lastDay = new Date(firstDay); lastDay.setDate(lastDay.getDate() + 6); console.log(firstDay); // 👉️ Monday August 8 2022 console.log(lastDay); // 👉️ Sunday August 14 2022

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

Get First and Last Day of Month Using JavaScript

Get First and Last Day of Month Using JavaScript

  1. Use the Date.getFullYear() Method in JavaScript to Get First and Last Day of Month
  2. Use the Date.getFullMonth() Method in JavaScript
  3. Get Last Day of the Month Using JavaScript
  4. Get the Particular Month’s First and Last Days Using JavaScript

This JavaScript tutorial tackles how to get the first and last day of the current month. For many projects, we need to find out the first and last date of the month.

On getting the first and last day of the current month in JavaScript, we first need to learn how to use the getFullYear() and getFullMonth() methods.

Use the Date.getFullYear() Method in JavaScript to Get First and Last Day of Month

The Date.getFullYear() method returns output for the year of the specified date according to the local time.

const randomDate = new Date('Jan 20, 2018 00:12:18'); console.log(randomDate.getFullYear()); 

Use Date.getFullYear Method in JavaScript

Use the Date.getFullMonth() Method in JavaScript

For the month, we will use the Date.getMonth() method and add 1 to the result to get the date for the next month. The getMonth() method returns the month as a zero-based value ( 0 indicates the year’s first month).

const randomDate = new Date('Jan 20, 2018 00:12:18'); console.log(randomDate.getMonth()); 

Use Date.getFullMonth Method in JavaScript

We must remember that a date’s month number is zero-indexed in JavaScript. So January will be month 0 and December month 11 .

Get Last Day of the Month Using JavaScript

We create a Date() object using the Date() constructor. The parameters will be the current year, current month + 1, and 0 for the day.

In addition, the Date object will contain the last day of the month. The three parameters we will be using for the Date() .

var my_date = new Date(); var first_date = new Date(my_date.getFullYear(), my_date.getMonth(), 1); document.write(first_date); var last_date = new Date(my_date.getFullYear(), my_date.getMonth() + 1, 0); document.write("
"
+last_date);

Get last day of the month using JavaScript

Above we can see that the code gives us the first and last date of the current month, but this code is only for the current date. If we want to manually input the date, we can pass our custom date as a parameter inside the Date() constructor.

Get the Particular Month’s First and Last Days Using JavaScript

Now suppose that we want to get the first and last day of a particular month. For example, let’s assume that we need to find the first day and the last day of January 2018.

Let’s look at the example below.

var my_date = new Date("2018, Jan"); var first_date = new Date(my_date.getFullYear(), my_date.getMonth(), 1); document.write(first_date); var last_date = new Date(my_date.getFullYear(), my_date.getMonth() + 1, 0); document.write("
"
+last_date);

This is balanced out because we passed 0 as the day parameter to the Date() constructor. So specifying a day of 0 means — giving us the last day of the month.

What happens is that we go one month forward by adding 1 to the return value from the getMonth method, and then we go one day back, to the last day of the month, by specifying 0 as the day.

Through this article, we first learned how to get the first and last day of the current month, and then we learned how to get the first and last day of a particular month using the Date.getFullYear() and Date.getMonth() method in JavaScript.

Related Article — JavaScript Date

Copyright © 2023. All right reserved

Источник

Читайте также:  Universal vertical center with CSS
Оцените статью