JavaScript: Get ISO-8601 week number of year, weeks starting on Monday
JavaScript Datetime: Exercise-24 with Solution
Write a JavaScript function to get ISO-8601 week number of year, weeks starting on Monday.
Example : 42 (the 42nd week in the year)
Test Data :
dt = new Date(2015, 10, 1);
console.log(ISO8601_week_no(dt));
44
Sample Solution:-
HTML Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript function to get ISO-8601 week number of year, weeks starting on Monday</title>
</head>
<body>
</body>
</html>
JavaScript Code:
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);
}
dt = new Date();
console.log(ISO8601_week_no(dt));
dt = new Date(2015, 10, 1);
console.log(ISO8601_week_no(dt));
Sample Output:
25 44
Flowchart:

Live Demo:
See the Pen JavaScript - Get ISO-8601 week number of year, weeks starting on Monday-date-ex-24 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript function to get English ordinal suffix for the day of the month, 2 characters (st, nd, rd or th.).
Next: Write a JavaScript function to get a full textual representation of a month, such as January or June.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
Converts a string to title case
Example:
const toTitleCase = str => str .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) .map(x => x.charAt(0).toUpperCase() + x.slice(1)) .join(' '); console.log(toTitleCase('some_database_field_name')); // 'Some Database Field Name' console.log(toTitleCase('Some label that needs to be title-cased')); // 'Some Label That Needs To Be Title Cased' console.log(toTitleCase('some-package-name')); // 'Some Package Name' console.log(toTitleCase('some-mixed_string with spaces_underscores-and-hyphens')); // 'Some Mixed String With Spaces Underscores And Hyphens'
Output:
"Some Database Field Name" "Some Label That Needs To Be Title Cased" "Some Package Name" "Some Mixed String With Spaces Underscores And Hyphens"
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework