Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create Date from string with week number in Javascript

I have a string like 2020-44 which contains year and number of the week in the year. Is it possible to create Date object from this string in some easy way? Javascripot is able to create dates from string like new Date(‘2020-12-24’). But I would like to use it with format 2020-44. Is it possible or not?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This will return the first day of the given week:

var dateString = "2020-44";
function getDateFromWeek(str) {
    var y = str.split('-')[0]
    var w = str.split('-')[1]
    var d = (1 + (w - 1) * 7); // 1st of January + 7 days for each week

    return new Date(y, 0, d);
}
console.log(getDateFromWeek(dateString));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading