Harshana Samarayaka
Harshana Samarayaka

Reputation: 13

How to get month and day from day count using java script?

if day count is 31, then month is 1 and day is 31.

if day count is 38, then month is 2 and day is 7.

if year is long year total days of year is 366 and if its not total days of year is 365.

so. Assume the year is not long year(365 days). So if i take day count is 334. the month is should be 11 (November) and day should be 30, But i can't get is . i followed following way.

function test(days){
var day;
var month;
if(days<=31){
    month=1;
    day=days;
}
else if(days<=59){
    month=2;
    day=days-31;
}
else if(days<=90){
    month=3;
    day=days-59;
}
else if(days<=120){
    month=4;
    day=days-90;
}
else if(days<=151){
    month=5;
    day=days-120;
}
else if(days<=181){
    month=6;
    day=days-151;
}
else if(days<=212){
    month=7;
    day=days-181;
}
else if(days<=243){
    month=8;
    day=days-212;
}
else if(days<=273){
    month=9;
    day=days-243;
}
else if(days<=304){
    month=10;
    day=days-273;
}
else if(days<=334){
    month=11;
    day=days-304;
}
else if(days<=365){
    month=12;
    day=days-334;
}

    alert(month);
    alert(day);

}

Upvotes: 0

Views: 280

Answers (6)

Wayne
Wayne

Reputation: 60414

You can determine the month using a lot less code (note, no bounds checking):

function getMonth(day) {
    var i = 0;
    var sum = 0;
    var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    while(sum < day) {
        sum += days[i++];
    }
    return i;
}

Or even:

function getMonth(day) {
    var i = 0, 
        days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    for (var sum = 0; sum < day; sum += days[i++]);
    return i;
}

Include the day of the month:

function getMonthAndDay(day) {
    var i = 0, sum = 0;
    var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    while(sum < day) {
        sum += days[i++];
    }
    return [i, day - sum + days[i - 1]];
}

Upvotes: 2

Sg&#39;te&#39;gmuj
Sg&#39;te&#39;gmuj

Reputation: 151

Keep It Simple Stupid (KISS):

var year = 2013
var day_of_year = 1

var d = new Date(year, 0, day_of_year)

var day_of_month = d.getDate()
var month_of_year = d.getMonth() + 1

console.log(day_of_month)
console.log(month_of_year)

ADDENDUM:

In regards to your question in the comments (I may be over tired though, because I don't see that comment anymore), to get the days in a given year, you can do the following:

var baseYear = 1992

var baseDate = new Date(baseYear, 0);
var extendedDate = new Date(baseYear + 1, 0, 0);

var daysInYear = 1 + ((extendedDate - baseDate) / 86400000)

console.log(daysInYear)

Upvotes: 4

Ray Toal
Ray Toal

Reputation: 88378

If anyone comes here looking to find the month and date from a year's day number, the best way is to use a preexisting library like momentjs.

Here's an example using node. After first running npm install moment, you can just do this:

> var moment = require('moment')
> var d = moment().dayOfYear(334)
undefined
> d.format()
'2013-11-30T23:45:36-08:00'
> [d.month()+1, d.date()]
[ 11, 30 ]

The +1 is because moment uses month numbers starting from 0.

The library takes care of daylight savings, time zones, leap years and similar things that you don't want to write in your own code, unless you are learning or practicing or having fun.

ADDENDUM

Sg'te'gmuj showed how to do this using plain JavaScript (no need for a third party library). Here is that solution as a function:

> function monthAndDay(year, dayOfYear) {
    var date = new Date(year, 0, dayOfYear);
    return [date.getMonth() + 1, date.getDate()]
  }

It's pretty sweet. Here it is in action, both for a leap year and for a non leap year:

> monthAndDay(2013, 334)
11,30
> monthAndDay(2012, 334)
11,29

Upvotes: 0

MaGnetas
MaGnetas

Reputation: 5008

var myDate=new Date();   //new date to get current year
myDate = new Date(myDate.getFullYear(), 0, 1);    //set to 1st of January (this year)

var x = 334;   //the day shift needed

myDate.setDate(myDate.getDate() + (x - 1));   //add the days to the date object we already built

var month = (myDate.getMonth() + 1);   //get the month. Add 1 since it's zero based
var day = myDate.getDate();   //get the day of month

alert(month + ' ' + day);   //do what you need to

Added whole process explanation to the comments of my code. It's possible to simplify this a little but I wanted to make sure you'd understand the whole algorithm.

Basically you take the beginning of the year, add the days you need and ask your object what month and day is set to it at the moment.

Upvotes: 1

Pascalz
Pascalz

Reputation: 2378

Another approach :

function test(dayCount) {
    var isLeapyear = false;
    var daysByMonth = [31,28,31,30,31,30,31,31,30,31,30,31];

    if (isLeapyear)
        daysByMonth = [31,29,31,30,31,30,31,31,30,31,30,31];

    var month = 1;
    var day = 1;

    var days = 0;

    for (i = 0; i < 12; i++) {       
        if ( days + daysByMonth[i] > dayCount)
        {
            day = (dayCount - days);
            break;
        }
        days += daysByMonth[i];    
        month++;
    }

    console.log('month : ' + month + ' - day : ' + day);
}

test(38);

Upvotes: 0

Rasmus
Rasmus

Reputation: 8476

It works fine for me .Test it here http://jsfiddle.net/PcZQ3/ . Just saw a typo in your code though . You have declared function as funtion .

Upvotes: 0

Related Questions