Hacker News new | past | comments | ask | show | jobs | submit login

Is it that you don't like zero-indexing, or that you don't like it together in the same function with one-indexing? b^)

Actually I think it's pretty terrible how few languages have literal expressions for dates. Once you have to type "new Date(", it's not much more annoying to have to remember that January is zero.




Apropos, you can easily make your own date syntax in Dart:

  class Month {
    static const List<String> names = const <String>[
      "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
      "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"                                                    
    ];
    final int month;
    const Month(this.month);
    MonthDay operator -(int day) => new MonthDay(month, day);
    String toString() => names[month - 1];
  }

  class MonthDay {
    final int month;
    final int day;
    MonthDay(this.month, this.day);
    DateTime operator -(int year) => new DateTime(year, month, day);
    String toString() => "${Month.names[month - 1]}-${"$day".padLeft(2, "0")}";
  }

  const Month JAN = const Month(1);
  const Month FEB = const Month(2);
  const Month MAR = const Month(3);
  const Month APR = const Month(4);
  const Month MAY = const Month(5);
  const Month JUN = const Month(6);
  const Month JUL = const Month(7);
  const Month AUG = const Month(8);
  const Month SEP = const Month(9);
  const Month OCT = const Month(10);
  const Month NOV = const Month(11);
  const Month DEC = const Month(12);

  main() {
    print(MAR-25-2015);
  }


Kudos! Truly, Dart has redeeming qualities! Incidentally, this code is the best argument I've seen for "American-style" date writing, with the month first. Or would it be as easy in Dart to override integers with a "DayMonth" operator?


I have no issue with zero-indexing. It is just inconsistent. Either everything should be zero index or nothing should. In this case nothing should.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: