Convert date to specific format julian date

조회 수: 5 (최근 30일)
Charlie Wood
Charlie Wood 2016년 10월 3일
편집: Charlie Wood 2016년 10월 4일
Need to convert calendar date to Julian date in a specific format (YYDDD). Ex: Day = (2016-05-04,2016-08-24,2016-12-31) J-Day = (16125,16237,16366)
Any help would be appreciated. Thanks.

채택된 답변

Gareth Thomas
Gareth Thomas 2016년 10월 3일
Datetime works nicely. I believe it came out in R2014b.
a = datetime('2016-05-04');
a.Format='yyddd'
  댓글 수: 4
Peter Perkins
Peter Perkins 2016년 10월 3일
If you are looking to get a string, Gareth was on the right track, just off by case:
>> a = datetime('2016-05-04')
a =
datetime
04-May-2016
>> char(a,'yyDDD')
ans =
'16125'
Of course, the whole point of datetime is to not have to convert between representations all the time, so perhaps
>> a.Format='yyDDD'
a =
16125
would serve as well. If you want a number, then your accepted answer is the way to go.
Charlie Wood
Charlie Wood 2016년 10월 4일
편집: Charlie Wood 2016년 10월 4일
Peter/Gareth, this is perfect. Switching the ddd to DDD was the fix needed in the a.Format.
>> a = datetime('2016-05-04');
>> a.Format='yyDDD'
a =
16125
This works for any year as well which is great. This will be new accepted answer vice the numerical version I put up.
Thanks a gain for the help!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Steven Lord
Steven Lord 2016년 10월 3일
Create a datetime array from the char vectors containing the yyyy-MM-dd form of the dates. Use the juliandate function to compute the Julian date from the datetime array.
  댓글 수: 1
Charlie Wood
Charlie Wood 2016년 10월 3일
편집: Charlie Wood 2016년 10월 3일
d = '2016-12-31';
dt = datetime(d)
dt = 31-Dec-2016
j = juliandate(dt)
j = 2.4578e+06
How do I get j into the yyddd format (to read 16366)?
Thanks.

댓글을 달려면 로그인하십시오.


Michael C.
Michael C. 2016년 10월 3일
My understanding was that Julian usually referred to the number of days since Jan 1 4713 BC, but judging by your example, you are using March 11th, 1972.
Either way, you can get number of days by doing a subtraction on two "datenum"s
datenum([2016 08 24 0 0 0]) - datenum([1972 3 11 0 0 0])
which gives the 16237. Passing in a string for the date will also work.
  댓글 수: 1
Charlie Wood
Charlie Wood 2016년 10월 3일
This worked pretty effectively. Unfortunately if I have a date from 2015 or 2014 (which I do), it doesn't work as well. I guess I can put an if statement in there to switch the second datenum to the right value. Thanks so much Michael.

댓글을 달려면 로그인하십시오.


Charlie Wood
Charlie Wood 2016년 10월 3일
Here's what I got combining some answers:
d = '2016-05-04';
dt = datime(d);
jday = (year(dt)-2000)*1000 + day(dt,'dayofyear')
The answer comes out with 16125. It's not pretty and not useful for dates before the year 2010 (maybe with some minor formatting it would be ok for 2000-2009). It does however work for my 2014-2016 dates so far.
Thanks to everyone for the help and input!!!

Ankit Kanthe
Ankit Kanthe 2016년 10월 3일
Here's what I got combining some answers:
d = '2016-05-04'; dt = datime(d); jday = (year(dt)-2000)*1000 + day(dt,'dayofyear') The answer comes out with 16125. It's not pretty and not useful for dates before the year 2010 (maybe with some minor formatting it would be ok for 2000-2009). It does however work for my 2014-2016 dates so far.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by