Date get week javascript

IamSilviu / Get week number

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Date.prototype.getWeek = function ()
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this — onejan) / 86400000) + onejan.getDay() + 1) / 7);
>;
var myDate = new Date(«2001-02-02»);
myDate.getWeek(); //=> 5

Nice. But if date includes non-zero hours or minutes, result is incorrect. This helps:

var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((new Date(this.getFullYear(), this.getMonth(), this.getDate()) — onejan) / 86400000) + onejan.getDay() + 1) / 7);

The same function with small refactoring:

function getNumberOfWeek()  const today = new Date(); const firstDayOfYear = new Date(today.getFullYear(), 0, 1); const pastDaysOfYear = (today - firstDayOfYear) / 86400000; return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); >

how to find week number in a month in javascript

how to find week number in a month in javascript

The same function with small refactoring:

function getNumberOfWeek()  const today = new Date(); const firstDayOfYear = new Date(today.getFullYear(), 0, 1); const pastDaysOfYear = (today - firstDayOfYear) / 86400000; return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); >

The same function with small refactoring:

function getNumberOfWeek()  const today = new Date(); const firstDayOfYear = new Date(today.getFullYear(), 0, 1); const pastDaysOfYear = (today - firstDayOfYear) / 86400000; return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); >
function getNumberOfWeek(date) < const firstDayOfYear = new Date(date.getFullYear(), 0, 1); const pastDaysOfYear = (date - firstDayOfYear) / 86400000; return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); >function ISO8601_week_no(dt) < var tdt = new Date(dt.valueOf()); var dayn = (dt.getDay() + 6) % 7; tdt.setDate(tdt.getDate() - dayn + 3); var firstThursday = tdt.valueOf(); tdt.setMonth(0, 1); if (tdt.getDay() !== 4) < tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7); >return 1 + Math.ceil((firstThursday - tdt) / 604800000); > console.log('============================='); dt = new Date(2015, 10, 1); < let time = performance.now(); console.log(ISO8601_week_no(dt)); //44 time = performance.now() - time; console.log('Время выполнения1 = ', time); > < let time = performance.now(); console.log(getNumberOfWeek(dt)); // 45 time = performance.now() - time; console.log('Время выполнения12 = ', time); >
function getNumberOfWeek()  const today = new Date(); const firstDayOfYear = new Date(today.getFullYear(), 0, 1); const pastDaysOfYear = (today - firstDayOfYear) / 86400000; return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); >

There is just small issue that typescript don’t validate arithmetic operations on Date type so shows below error.

The left-hand side of an arithmetic operation must be of type ‘any’, ‘number’ or an enum type

So I’ve called .valueOf() on Dates in that arithmetic operation.

getNumberOfWeek(): number  const today = new Date(); const firstDayOfYear = new Date(today.getFullYear(), 0, 1); const pastDaysOfYear = (today.valueOf() - firstDayOfYear.valueOf()) / 86400000; return Math.ceil((pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7); >

Источник

Get Week Number of the Year in JavaScript

Get Week Number of the Year in JavaScript

  1. Use the getFullYear() and getDay() Functions Along With the new Date() ’s Object to Get the Week Number of the Current Date
  2. Use the DateTime Data Structure and Its weekNumber Properties From Luxon to Get the Current Week Number of the Year

We will introduce a method to find the current week number of the year in JavaScript, creating the object of the new Date() constructor and the functions like getFullYear() and getDay() . This method will find the total number of days of the year and find the week number. We will use the Math object’s functions like floor() and ceil() in this method.

This article will also introduce another method of getting the current week number in JavaScript using a javascript wrapper Luxon. Luxon is a library of JavaScript dates and times. We will use the DateTime data structure and the weekNumber property from this library. We will also use the now function with the DataTime data structure.

We will also demonstrate a method to declare a global variable in PHP using the define() function. This method is a way of declaring a constant global variable in PHP. The value of the variable cannot be changed later.

Use the getFullYear() and getDay() Functions Along With the new Date() ’s Object to Get the Week Number of the Current Date

This method uses the new Date() constructor and its object along with the functions like getFullYear() and getDay() to get the current week number of the year. The Date object returns the current date. The getDay() function finds the current day of the week in an integer value. We can create an object of the Date() constructor to get the current date. The object calls the function getFullYear() to get the start of the current year. We can find the total number of days from the start of the day to the current time by dividing the days’ difference by the total milliseconds in a day. We can use the floor() function of the Math object to round off the value to get a whole number. This method finally calculates the week number of the year, dividing the total number of the days by seven.

For example, create a Date object currentdate . Create another variable, oneJan , to store the first day of the current year. For that, create a new Date object on the variable and call the getFullYear() function with the currentdate object as the first parameter of the Date() constructor. Use 0 and 1 as the second and the third parameter. Subtract the variable oneJan from currentdate and divide it by 86400000 . Wrap the operations inside Math.floor() function and assign it to a variable numberOfDays . Call the getDay() function with the currentdate object and add the variable numberOfDays and the value 1 to it. Wrap this operation with the Math.ceil() function and store the result in the result variable. Log the variable in the console along with the currentdate variable using string interpolation.

The example below finds the current date and then the first day of the current year. The parameters 0 and 1 in the second line represents the first month and the first day of the current year. Subtracting the oneJan from the currentdate gives the value in milliseconds, so the total milliseconds in a day which is 86400000 divides the difference. The getDay() function returns the day in integer form starting with 0 , so we add 1 . Therefore, the current week number is calculated.

currentdate = new Date(); var oneJan = new Date(currentdate.getFullYear(),0,1); var numberOfDays = Math.floor((currentdate - oneJan) / (24 * 60 * 60 * 1000)); var result = Math.ceil(( currentdate.getDay() + 1 + numberOfDays) / 7); console.log(`The week number of the current date ($currentdate>) is $result>.`); 
The week number of the current date (Tue May 25 2021 16:55:53 GMT+0545 (Nepal Time)) is 21. 

Use the DateTime Data Structure and Its weekNumber Properties From Luxon to Get the Current Week Number of the Year

We can use the Luxon library to get the current week number of the year with the DateTime data structure and weeknumber property. The DateTime consists of a timestamp, a time zone, and configuration properties. The DateTime used with the now function will return the date and time for the current instant in the system’s timezone. The weekNumber property accesses the week number of the current year. We can use the toISO() function to convert the DateTime to the ISO format.

Import the DateTime object from the luxon/src/datetime.js in the first line. Do not forget to download the Luxon source code. Save the source code in a JavaScript file and make sure to include the file using the script tag in the HTML section. Call the now() function with the DateTime object and then call the weekNumber property. Assign it to a variable date . Then, log the variable date in the console. Please consult the Luxon Documentation to know more about the DateTime objects and properties.

import DateTime from 'luxon/src/datetime.js' const date = DateTime.now().weekNumber console.log(`The current week number is $date>`) 
The current week number is 21 

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

Related Article — JavaScript DateTime

Copyright © 2023. All right reserved

Источник

Date get week javascript

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Читайте также:  Cryptopro ru sites default files products cades demopage cades bes sample html
Оцените статью