Convert yyyymmdd date to yyJJJ or Julian Date

Need a formula in excel and C # to convert data in "yyyymmdd" format to julian date yyJJJ format? Where "JJJ" is the Julian date of the day. Is JJJ a number from 1 to 365 or 999 or 366? I don't think its 366, but we really have a leap year, so it could be a leap year.

In Excel, I am trying to use the following:

=Year() & DAYS360("20110101","20110930") 

In C #, I use now () to get the current date. But I need the number of days between January 1 and the current date. Not sure if this is the correct prototype formula:

 Prepend Two digit year format to the following Number of Days between "20110101" and "20110930" in "YY" format 
+6
source share
4 answers

Instead of handling this yourself in C #, you can simply use the JulianCalendar class

And to get into the Julian calendar today, you can do:

 JulianCalendar calendar = new JulianCalendar(); var today=DateTime.Today; var dateInJulian = calendar.ToDateTime(today.Year, today.Month, today.Day, today.Hour, today.Minute, today.Second, today.Millisecond); 

EDIT:

And I realized that I didn’t help you with a good way to get the result in the YYJJJ format you were looking for, this is pretty simple:

 var stringResult=string.Format("{0}{1}", dateInJulian.ToString("yy"), dateInJulian.DayOfYear); 

Unfortunately, I do not think there is an equivalent to dateTime.ToString("JJJ"); which will give the day of the year, which means my little string.Format() work-around, but it works just as well!

EDIT2:

So, Phog reinforced me a bit in the comments and noted that you did not look today in the Julian calendar, which was said above in relation to the JulianCalendar class. Rather, you seem to be looking for the date of the ordinal (this snippet has been added to further clarify the reader). I am going to leave above so that someone does not make the same mistake as me. I believe that you really need the code from the first edit, but without the JulianCalendar business, therefore:

 var dateToConvert = DateTime.Today // Or any other date you want! var stringResult=string.Format("{0}{1}", dateToConvert.ToString("yy"), dateToConvert.DayOfYear); 
+11
source

In accordance with these articles, you should use the term "Original Date" and not "Julian Date":

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

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

According to the first ISO standard, a four-digit year and a range of 1 to 366 are indicated for the ordinal date, so January 1 is 1.

This, of course, means that the dates between March and December will have two possible meanings, depending on whether the date falls in a leap year.

A Bala solution (with option + 1 given in the comments) will suffice.

EDIT: The DayOfYear property of the DateTime type, as Jim Michel noted, is simpler and probably faster.

+5
source

in c # you can do

 DateTime dt = DateTime.Now; DateTime firstJan = new DateTime(dt.Year, 1, 1); int daysSinceFirstJan = (dt - firstJan).Days + 1; 
+1
source

I believe what you are looking for:

 DateTime dt = DateTime.Now; string ordinalDate = dt.ToString("yy") + dt.DayOfYear.ToString("000"); 

This will give you a string that is always 5 digits with a two-digit year and a 3-digit day of the year. Keveks answer will give you a string that can be 3, 4 or 5 digits.

+1
source

Source: https://habr.com/ru/post/898381/


All Articles