Nick
Nick

Reputation: 4493

How to convert date to Julian date in Dart or Flutter?

I need to send data to mainframe as Julian date. But I am not sure how to do it in Flutter or Dart. There is a plugin but seems is not compatible with Dart2. So what is the generic version to convert date to Julian date?

Upvotes: 4

Views: 1476

Answers (1)

lrn
lrn

Reputation: 71743

There are a number of packages that may help you, but I'm not personally familiar with them. It is easily computable using the DateTime and Duration classes in Dart, e.g.:

// Julian day zero started at noon, November 24, 4714 BC
// in the proleptic Gregorian calendar. 
// (Since 1 BC is year 0, we use -4713 for the year here)
final julianEpoch = DateTime.utc(-4713, 11, 24, 12, 0, 0);
int julianDayNumber(DateTime date) =>
    date.difference(julianEpoch).inDays;
double julianDay(DateTime date) =>
    date.difference(julianEpoch).inSeconds / Duration.secondsPerDay;
DateTime dateFromJulianDay(num julianDay) => 
    julianEpoch + Duration(milliseconds: (julianDay * Duration.milliSecondsPerDay).floor());
double modifiedJulianDay(DateTime date) => julianDay(date) - 2400000.5;

Upvotes: 6

Related Questions