开发者

How do you convert a DateTime to JulianDate?

开发者 https://www.devze.com 2023-03-01 14:19 出处:网络
I need to convert a DateTime to a julian date in YYDDD format. How do you do it? I have seen there is a JulianCalendar class in .net but cannot find any example how to do the conversion.

I need to convert a DateTime to a julian date in YYDDD format. How do you do it? I have seen there is a JulianCalendar class in .net but cannot find any example how to do the conversion. I have seen few links but none of them work.

any suggestions?

UPDATE I have been giv开发者_如何学编程en some date to convert to a csv file and this include converting a dateTime to a format that i cannot change apparently is used in the banking world. format is YYDDD .After some googling it looked like it was julian calendar. I might be wrong.So here iam an totally confused of what format that is.


It looks like this format is indeed also called Julian Date in the mainframe world...

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=julian+date+yyddd

var myDate = DateTime.Now;
string.Format("{0:yy}{1:D3}", myDate, myDate.DayOfYear);

EDIT - To convert such a string back to a date, one can use something like this:

DateTime FromBizarreMainframeFormat(string julianDate) {
    // TODO add validation logic, plus rules to cope with pre-2000 dates
    var yy = 2000 + int.Parse(julianDate.SubString(0,2));
    var ddd = int.Parse(julianDate.Substring(2));
    return new DateTime(yy, 1, 1).AddDays(ddd);
}


It looks like you are asking for a value equal to (myDate.Year % 100) * 1000 + myDate.DayOfYear. That is a pretty simple calculation to do.

If you want a string with leading zeros, then you could do (myDate.Year % 100).ToString("D2") + myDate.DayOfYear.ToString("D3").

0

精彩评论

暂无评论...
验证码 换一张
取 消