Francesco Papa
Francesco Papa

Reputation: 1

Retrieve number of day with my string

I want to say the number of day with a date. I have my string; var miadata = 20150925 and I want to say the number of day.

Example:

Help me.

Upvotes: 0

Views: 23

Answers (2)

Sahil
Sahil

Reputation: 3358

Hi you would first have to make a date out of the string:

var miadata = "20150925";

returnDay(miadata); 

function returnDay(string){
 var yyyy = string.substr(0,4);
 var mm = string.substr(4,2);
 var dd = string.substr(6,2);
 var date = new Date(yyyy+'-'+mm+'-'+dd);
 var dayNum = date.getDay();//returns 0-6, where 0-Sunday, 1-Monday and so on..
 var days = ["Domenica","Lunedi","Martedi","Mercoledi","Giovedi","Venerdi","Sabato"];
console.log("Day is "+days[dayNum]);
}

Upvotes: 1

Axel Lavielle
Axel Lavielle

Reputation: 252

You need a base date, something where you know which day it is like: var data=19800106 which is a sunday (Domenica).

From this date you can calculate your actual day: calculate the difference of days between the two dates (care of the bissextile years every 4 years) and do a modulo on the result:

NbrdayMiaData - NbrDayData = nbrday;
nbrday % 7 = The number you are searching (0, 1, ..., 6).

And then with an array you can easily compare the number to the string.

Upvotes: 0

Related Questions