Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
day of year to month

Subject: day of year to month

From: Dave

Date: 27 Jul, 2009 20:52:01

Message: 1 of 11

hi,

simple one...

i have lots of data points taken on various days of the year, 1-365 or 366 on a leap year. i have been trying to write an m-file to look at each day, figure out which month its in and then return that month number in another column, with no luck.

can someone please let me know the best/simplest way to do this?

Thanks a bunch!

d

Subject: day of year to month

From: Alan B

Date: 27 Jul, 2009 21:09:02

Message: 2 of 11

"Dave " <yogi_cave@yahoo.com> wrote in message <h4l41h$qv5$1@fred.mathworks.com>...
> hi,
>
> simple one...
>
> i have lots of data points taken on various days of the year, 1-365 or 366 on a leap year. i have been trying to write an m-file to look at each day, figure out which month its in and then return that month number in another column, with no luck.
>
> can someone please let me know the best/simplest way to do this?
>
> Thanks a bunch!
>
> d


This should be pretty simple for a given year:

whichMonth = min(find(julianDay<cumsum(daysInMonths)));

but slightly tougher when you have to figure out how many days there are in February.

For the least amount of work for you to do, use datenum and datestr:

whichMonth = datestr(datenum('1/1/2009')+julianDay,'mm');

In short, a matlab datenum increases by 1 for each day, starting at 0 on day '1/0/0' or January 0, year 0.

Subject: day of year to month

From: Steven Lord

Date: 27 Jul, 2009 21:17:49

Message: 3 of 11


"Alan B" <monguin61REM@OVETHIS.yahoo.com> wrote in message
news:h4l51e$15t$1@fred.mathworks.com...
> "Dave " <yogi_cave@yahoo.com> wrote in message
> <h4l41h$qv5$1@fred.mathworks.com>...
>> hi,
>>
>> simple one...
>>
>> i have lots of data points taken on various days of the year, 1-365 or
>> 366 on a leap year. i have been trying to write an m-file to look at each
>> day, figure out which month its in and then return that month number in
>> another column, with no luck.
>>
>> can someone please let me know the best/simplest way to do this?
>>
>> Thanks a bunch!
>>
>> d
>
>
> This should be pretty simple for a given year:
>
> whichMonth = min(find(julianDay<cumsum(daysInMonths)));
>
> but slightly tougher when you have to figure out how many days there are
> in February.
>
> For the least amount of work for you to do, use datenum and datestr:
>
> whichMonth = datestr(datenum('1/1/2009')+julianDay,'mm');
>
> In short, a matlab datenum increases by 1 for each day, starting at 0 on
> day '1/0/0' or January 0, year 0.

Depending on whether or not the OP wants the date number as a string (like
'07') or a number (7) they might want to use DATEVEC instead of DATESTR.

--
Steve Lord
slord@mathworks.com

Subject: day of year to month

From: James Tursa

Date: 27 Jul, 2009 22:23:01

Message: 4 of 11

"Alan B" <monguin61REM@OVETHIS.yahoo.com> wrote in message <h4l51e$15t$1@fred.mathworks.com>...
> "Dave " <yogi_cave@yahoo.com> wrote in message <h4l41h$qv5$1@fred.mathworks.com>...
> > hi,
> >
> > simple one...
> >
> > i have lots of data points taken on various days of the year, 1-365 or 366 on a leap year. i have been trying to write an m-file to look at each day, figure out which month its in and then return that month number in another column, with no luck.
> >
> > can someone please let me know the best/simplest way to do this?
> >
> > Thanks a bunch!
> >
> > d
>
>
> This should be pretty simple for a given year:
>
> whichMonth = min(find(julianDay<cumsum(daysInMonths)));
>
> but slightly tougher when you have to figure out how many days there are in February.
>
> For the least amount of work for you to do, use datenum and datestr:
>
> whichMonth = datestr(datenum('1/1/2009')+julianDay,'mm');
>
> In short, a matlab datenum increases by 1 for each day, starting at 0 on day '1/0/0' or January 0, year 0.

A bit of a side issue, but you have hit one of my pet peeves. The term Julian Day should be reserved for number of days since January 1, 4713 BC Greenwich noon. e.g., see here:

http://en.wikipedia.org/wiki/Julian_day

Please do not use Julian Day to refer to day of the year. This is a misuse of the term. You are not the first to do this on this newsgroup, and I have found submissions in the FEX that misuse JD as day of the year also. Just call it something else. Thanks.

James Tursa

Subject: day of year to month

From: Alan B

Date: 27 Jul, 2009 23:10:18

Message: 5 of 11

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <h4l9c5$918$1@fred.mathworks.com>...
> "Alan B" <monguin61REM@OVETHIS.yahoo.com> wrote in message <h4l51e$15t$1@fred.mathworks.com>...
> > "Dave " <yogi_cave@yahoo.com> wrote in message <h4l41h$qv5$1@fred.mathworks.com>...
> > > hi,
> > >
> > > simple one...
> > >
> > > i have lots of data points taken on various days of the year, 1-365 or 366 on a leap year. i have been trying to write an m-file to look at each day, figure out which month its in and then return that month number in another column, with no luck.
> > >
> > > can someone please let me know the best/simplest way to do this?
> > >
> > > Thanks a bunch!
> > >
> > > d
> >
> >
> > This should be pretty simple for a given year:
> >
> > whichMonth = min(find(julianDay<cumsum(daysInMonths)));
> >
> > but slightly tougher when you have to figure out how many days there are in February.
> >
> > For the least amount of work for you to do, use datenum and datestr:
> >
> > whichMonth = datestr(datenum('1/1/2009')+julianDay,'mm');
> >
> > In short, a matlab datenum increases by 1 for each day, starting at 0 on day '1/0/0' or January 0, year 0.
>
> A bit of a side issue, but you have hit one of my pet peeves. The term Julian Day should be reserved for number of days since January 1, 4713 BC Greenwich noon. e.g., see here:
>
> http://en.wikipedia.org/wiki/Julian_day
>
> Please do not use Julian Day to refer to day of the year. This is a misuse of the term. You are not the first to do this on this newsgroup, and I have found submissions in the FEX that misuse JD as day of the year also. Just call it something else. Thanks.
>
> James Tursa

Good to know.

Subject: day of year to month

From: Sprinceana

Date: 28 Jul, 2009 08:14:01

Message: 6 of 11

>> julianDay=20

julianDay =

    20

>> whichMonth = datestr(datenum('1/1/2009')+julianDay,'mm');
>> whichMonth

whichMonth =

01

>>

Subject: day of year to month

From: Steven Lord

Date: 28 Jul, 2009 13:43:42

Message: 7 of 11


"Sprinceana " <mihaispr@yahoo.com> wrote in message
news:h4mc09$gdu$1@fred.mathworks.com...
>>> julianDay=20
>
> julianDay =
>
> 20
>
>>> whichMonth = datestr(datenum('1/1/2009')+julianDay,'mm');
>>> whichMonth
>
> whichMonth =
>
> 01
>
>>>

You've got an off-by-one error there.

theday = 31;
whichMonth = datestr(datenum('1/1/2009')+theday,'mm')
whichMonth2 = datestr(datenum(2009, 1, theday), 'mm')

Yes, it's a little weird to talk about something like the 70th day of
January, but it works.

--
Steve Lord
slord@mathworks.com

Subject: day of year to month

From: us

Date: 28 Jul, 2009 13:50:19

Message: 8 of 11

"Steven Lord"
> Yes, it's a little weird to talk about something like the 70th day of
> January, but it works.

ahhh, steve... finally(!): an explanation why my daughters NEVER celebrate my birthday...

urs

Subject: day of year to month

From: Steven Lord

Date: 28 Jul, 2009 13:57:14

Message: 9 of 11


"us " <us@neurol.unizh.ch> wrote in message
news:h4mvmr$401$1@fred.mathworks.com...
> "Steven Lord"
>> Yes, it's a little weird to talk about something like the 70th day of
>> January, but it works.
>
> ahhh, steve... finally(!): an explanation why my daughters NEVER celebrate
> my birthday...
>
> urs

Are you sure they're not celebrating and you just don't notice because
you're on CSSM all day? :)

--
Steve Lord
slord@mathworks.com

Subject: day of year to month

From: Dave

Date: 28 Jul, 2009 19:17:02

Message: 10 of 11

wow-e-wow!

Thanks to all...

I'm sure the answer is there in many forms!

much appreciated

d

Subject: day of year to month

From: Jayandra Shrestha

Date: 2 Sep, 2013 09:38:07

Message: 11 of 11

function [M, D] = DayNum2Month_Day(Year, DayNum)
    mDays = eomday(Year, 1:12);
    cDays = [0 cumsum(mDays)];
    
    i = find(cDays>=DayNum);
    mi = i(1);
    M = mi-1;
    
    D = DayNum - cDays(mi-1);
end

Example:
>> [m, d]= DayNum2Month_Day(2008,200)
m =
     7
d =
    18

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us