4 ms·
Just today I was reminded of how terrible JS is: new Date(2015, 1, 1) Guess what date that object represents (hint: It isn't January 1st 2015).
by Someone1234 12y ago
Just today I was reminded of how terrible JS is:
new Date(2015, 1, 1)
Guess what date that object represents (hint: It isn't January 1st 2015).
- jessaustin 12y agoIs 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.
- Someone1234 12y agoI have no issue with zero-indexing. It is just inconsistent. Either everything should be zero index or nothing should. In this case nothing should.
- reichstein 12y agoApropos, 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); }
- jessaustin 12y agoKudos! 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?
- alblue 12y agoTo be fair to JavaScript, whilst it is a terrible language, the specifics of the Date API were almost certainly based on the designs of the Java Date class, which is possibly the worst class known to humankind. http://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api http://stackoverflow.com/questions/1969442/whats-wrong-with-...
- tedmielczarek 12y agoBrendan has confirmed many times that they were instructed to copy the Java Date API exactly. Here's one: https://brendaneich.com/2011/06/new-javascript-engine-module-owner/ https://brendaneich.com/2011/06/new-javascript-engine-module... "Ken Smith, a Netscape acquiree from Borland, ported JDK 1.0-era java.util.Date (we both just drafted off of the Java truck, per management orders; we did not demur from the Y2K bugs in that Java class)."
- mswift42 12y agoYes, that's the strangest, most inconsistent model anyone could come up with.
- chc 12y agoThey could use the Japanese calendar. new Date(Heisei, 27, 1, 1)
- TazeTSchnitzel 12y agoAny function with positional parameters can look awful taken out of context.
- Someone1234 12y agoCan you clarify what you mean? The three parameters are year, month, and day. Which I think is fairly intuitive. The problem is not with parameter ordering. Year and day are literal, month is zero indexed. So you wind up with (month -1) representing the actual month.
- babuskov 12y agoI assume that first versions of JavaScript were written in C, and that's where this comes from: http://www.cplusplus.com/reference/ctime/tm/ http://www.cplusplus.com/reference/ctime/tm/ tm_mday = day of month (1-31) tm_mon = months since January (0-11) Which AFIAK goes all the way back to POSIX standard. The real problem is that JavaScript as higher level language decided to inherit this awkward convention instead of making their own, consistent rules. Given the time it took to create the first JS implementation, it is no wonder that some shortcuts were taken.
- timepiece 12y agoDon't you know already the rules governing date-time in js? It seems to me that you are not doing your homework and studying hard to get things done professionally.