0

I'm not looking for something powerful, for date format DateFormat is enough but i need to add A.D or B.C like in Anno Domini for dates.

Now i know it's not even a challenge to customize it but I'm looking for best way to do it with the official API so it can adapt to changes in the future.

OverLoadedBurden
  • 606
  • 1
  • 5
  • 14
  • @jamesdlin That does it thanks !! but add it as an accepted answer so others may see it, couldn't figure out that i show search for `era designator` in the documentation. – OverLoadedBurden Jun 05 '21 at 22:21

1 Answers1

1

package:intl's DateFormat class should be able to do with the G formatter:

import 'package:intl/intl.dart';

void main() {
  var date = DateTime(2021, 5, 6);
  print(DateFormat('yyyy-MM-dd G').format(date)); // Prints: 2021-05-06 AD
}

Note that negative years seem to be directly assumed to be BC; that is, a year of -1 seems to be treated as 1 BC. This might not be what you expect if you want year 0 to be invalid.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • It should be a problem but according to https://en.wikipedia.org/wiki/Year_zero "Historians have never included a year zero." so if the system wasn't abused it won't be a problem – OverLoadedBurden Jun 05 '21 at 22:25
  • @OverLoadedBurden Well, if you try to compute durations (e.g. by subtracting a BC year from an AD year), then your duration will be off by 1 year. – jamesdlin Jun 05 '21 at 22:31
  • you are right, but the system cares for original making date of old artifacts. so, am on the safe side. – OverLoadedBurden Jun 05 '21 at 22:34