## Thursday 14 September 2017

### Calculate Epi Week from Date | Convert Epi Week to Date

Here we will be discussing about how to calculate an Epi Week from a given date and also about how to convert Epi Week to a date.

1. Calculate Epi Week from a given date.
2. Convert Epi week back to date.
3. Regex for Epi Week validation.

#### What is an "Epi Week"?

It is basically referred to an epi Week or cdc week, which simply a standard way to count the week. It can change depending on the specific requirement. Check below link to see Epi Week calendar.

http://www.cmmcp.org/2016EPI.html.

If you want to read more about Epi Week check this link as well :- http://www.cmmcp.org/epiweek.html

You can update the year in the URL to get specific year calendar. Let us check how we can calculate epi week from a given date using JavaScript. Before that check some examples of epi Week listed below.

#### Epi Week Examples

``````
Jan 01 2014: 1 2014 (http://www.cmmcp.org/2014EPI.htm.)
Jan 01 2015: 53 2014 (http://www.cmmcp.org/2015EPI.htm.)
Jan 10 2015: 1 2015 (http://www.cmmcp.org/2015EPI.htm.)
Jan 01 2016: 52 2015 (http://www.cmmcp.org/2016EPI.htm.)
Jan 01 2017: 1 2017 (http://www.cmmcp.org/2017EPI.htm.)
```
```

Basically we will consider Sunday as the first day and any week start after Thursday will be considered another week (count + 1 0r -1). This can be updated according to your requirement easily. I will share those info below.

#### Calculate Epi Week from a given date.

This method accept date in MMM DD YYYY format (Ex: Jan 01 2016). You can use JavaScript or moment js to convert date into any format. For example using moment js.

``````
moment(date).format('MMM DD YYYY');
or
moment.utc(date).format('MMM DD YYYY');
```
```

If you are using Angular you can convert the date format easily, read here for more info. Now check our below method to calculate epiweek from a given date.

``````
calculateEpiWeekFromDate("Jan 01 2016");
```
```

``````
function calculateEpiWeekFromDate(value)
{
Date.prototype.getWeek = function ()
{
var target = new Date(this.valueOf());

var dayPs = (this.getDay() + 7) % 7;

target.setDate(target.getDate() - dayPs + 3);

var jan4 = new Date(target.getFullYear(), 0, 4);

var dayDifference = (target - jan4) / 86400000;

if (new Date(target.getFullYear(), 0, 1).getDay() < 4)
{
return 1 + Math.ceil(dayDifference / 7);
}
else
{
return Math.ceil(dayDifference / 7);
}
};

var weekNumber = new Date(value).getWeek()

var year = getYear(value, weekNumber);

return weekNumber + ' ' + year;

}
```
```

``````
//For getting year for epi Week
function getYear(value, weekNumber)
{
var year = parseInt(value.split(' ')[2]);
if (value.split(' ')[0] == 'Jan') {
if (weekNumber > 40) {
year = year - 1;
}
}
if (value.split(' ')[0] == 'Dec') {
if (weekNumber < 2) {
year = year + 1;
}
}
return year.toString();
}
```
```

You can update this code according to your requirement.

#### Convert Epi Week to date.

First up all it is not possible to identify the exact date. It can be any date in that particular week. So here we will calculate the first Sunday of that week. Trust me this is going to help you for in many scenarios.

``````
//w = week number(Ex:12),y = year(Ex:2016)
function getDateFromWeek(w, y)
{
var _days;
if(w == 53){
_days = (1 + (w - 1) * 7);
}else{
_days = (w * 7);
}
var _date = new Date(y,0,_days);
_date.setDate(_date.getDate() - _date.getDay());
return _date;
}
```
```

For testing this convert a date into Epi Week and convert it back using above code.

#### Regex for Epi Week validation (ex: 32 2014)

In case if you want to validate Epi week using regex, you can use below mentioned regex.

Regex for 0 to 53.

``````
^([0-4]?[0-9]|5[0-3])\s\d{4}\$
```
```

If you don’t want to include 00, 01

``````
^([0-9]|[1-4][0-9]|5[0-3])\s\d{4}\$
```
```

If you don’t want zero to be part of Epi Week

``````
^([1-9]|[1-4][0-9]|5[0-3])\s\d{4}\$
```
```

You can test your regex here: - https://regex101.com/

In this Article we have discussed about, converting a date into an Epi Week, converting Epi Week back to a date and also about validating Epi week using Regex.