* Immutable
* Months in Luxon are 1-indexed instead of 0-indexed like in Moment and the native Date type.
* Localizations and time zones are implemented by the native Intl API (or a polyfill of it), instead of by the library itself.
* Luxon has both a Duration type and an Interval type. The Interval type is like Twix.
* Luxon lacks the relative time features of Moment and will until the required facilities are provided by the browser.
For what it's worth, there's no code (edit: well, maybe a few lines) in common between the libraries; Luxon is written from scratch. I haven't done a ton of perf testing, but depending on your use case, I'd expect most operations to be much faster in Luxon. The caveat there is that Moment objects are mutable, which can sometimes be a perf advantage.
I think there are lots of good reasons to make datetimes immutable (e.g. many of the same reasons most languages make strings immutable). But here's one specific to Moment and Luxon: chainable APIs should really always have immutable types because the return value is the thing you use next anyway. When each call returns an instance of the type it's both completely unnecessary and very often harmful for those calls to modify anything.
var a = moment();
console.log("the end of tomorrow is", a.add(1, 'day').endOf('day'));
// do more stuff with a
// oh crap, a is now set to tomorrow? wtf?
If you want a to be the end of tomorrow, all you needed to do was assign a to the whole chain, so this mutation didn't help you. That add() and endOf() return instances of Moment signals that they are Moment -> Moment functions, not mutators that also return the object. A mutable API should have void methods:
var a = moment();
a.add(1, 'day');
a.endOf('day');
// do stuff with a
Dates and times are values by definition, not mutable objects that can be changed via methods. The year 2017 is 2017. It represent a moment in time, or more precisely a period of time between two points. The year 2017 does not “become” 2018 when you .add one year to it. Fully immutable objects are also easier and simpler to compare since they are, again, values. 2017 is 2017, always. Not using proper immutability leads to an “object identity” crisis where everything is anything. An infinite amount of years 2017 are allowed to exist by default(!) and they are not easily comparable or require specific library APIs to allow that.
When can you make a Python version available? Datetime is by far my biggest gripe with Python. It takes many times the lines of code to do something “simple” with it vs other languages (e.g. parse remote time stamp in a specific format, apppend time zone info to that, get local time + zone, normalize both to UTC, compare, present difference in human-readable format).
Have you tried arrow (my favorite), dolorean or a third one the name escapes me right now?
I use arrow whenever I need to process a date, no matter the usage. Having a consistent interface is golden.
EDIT I just realized that the author of moment in Python (you link to) also mentions arrow.
I have to remember the name of the third package, it addressed some weaknesses in arrow (particularly issues with failed parsing which does not output an error)
I'm not much of a Python person, but it seems like someone could port it without too much trouble. In particular, you wouldn't have to use the ugly tricks I used to make the time zone stuff work
No, there's no plan to do that. The trouble is that Luxon has no idea how the Hebrew calendar works; it only knows how to output the non-Gregorian stuff because your browser already can via the Intl.DateTimeFormat API. Luxon finds some interesting ways to abuse that API to add some neat features like internationalized parsing and full time zone support, but there's a limit there. To add 7 Hebrew months, Luxon would actually have to be able translate arbitrary Hebrew dates into timestamps, and I can't think of a way to make it do that without any real knowledge of the calendaring system.
I've migrated a lot of code from moment to date-fns because of moment's bulky size and vast, mutable API. Luxon seems to fix the API issue. date-fns is still great for simple operations, but I'll definitely consider this for new projects with nontrivial datetime handling.
That any computers talk to each other about time in any other terms than epoch is insane to me. From there, converting it to local time zone is pretty easy, but it should be a presentation-layer ONLY thing. If you’ve ever worked with or implemented a system where this wouldn’t work, I’m interested in hearing about why.
If you schedule a meeting in the future you typically agree on a local time in a specific timezone. So the (local time, timezone) tuple is the authoritative value you want to store (you can cache the utc time if you want).
Just storing the utc time and timezone is problematic, since the conversion function between utc and local can change via tz database updates, sometimes with little notice (Israel is notorious for sometimes only deciding on the DST switching date shortly before it happens).
In principle you could store (utc-time, offset used to convert from local, timezone), but I find that rather unintuitive and error prone, since it doesn't cleanly separate authoritative from cached values.
Ideally, yes. There are a lot of times you need it though:
1. Recurring meeting events have to take place in a certain zone so that the right epoch time can be calculated for the recurrences across DSTs
2. Even single meetings need them (see other post in this thread)
3. You may need other zone's local times. Imagine you're scheduling shifts for people to work. If you're in a different zone than work is scheduled in, the presentation later needs to know that zone to compute the right epoch times.
Ah, sorry. I was speaking more in terms of internationalization than actually converting between time zones. All you need is a database, but that database can be hard to maintain and can be very large.
I'd love to make it smaller. Several of the builds contain polyfills for newish JS features not supported in IE. I'd like to strip that out and shave off a few k.
EDIT: the official “kibibyte” was standardized in 1998: https://physics.nist.gov/cuu/Units/binary.html so almost 20 years ago. Blame Windows and HDD mfgs for sticking with the SI definitions.
Windows (at least up to 8) does not follow the SI standard. It uses KB, MB, GB but converts using 1024. Which has funny consequences, like 999MB being smaller 0.94GB.
I think that moment.js was already built that way. For me it kinda makes sense: if days are calculated with a 1-index in mind, why make months different? As I said: it KINDA makes sense.
No, they're 0-indexed in Moment. But yes, the reason I made Luxon 1-index them is that days and years are 1-indexed. I also fielded a lot of issues in Moment that were caused by newer programmers not knowing that months were 0-indexed.
I'm not defending the difference between how months and days/years are treated. But here are some possible reasons.
From ctime(3):
Broken-down time is stored in the structure tm, which is defined in <time.h> as follows:
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
Well, nobody is confused when their digital watch reads "00:00:00" as the ball drops on New Years Eve. But if the date was 2018-00-01 people would be like "WTF??"
As explained on the website, that's why this is Luxon and not moment 3.0. It's a great approach for them to take that allows them to experiment, grow, and relieve the pressure from calls for change, without disrupting the existing community.
Thanks. This link should be on the github page to be honest. For people like me who do not (always) click through if they don’t see an immediate need. Only now notice the url contains moment and this is part of moment.
For me and I'm sure others one of the most important differences is immutability. This has been a big request from the community, but would be impossible to implement in moment without breaking much code which relies on the current mutability.
Biggest remaining difference I can see is that Luxon still has it's own classes while date-fns operates on bare DateTime objects (AFAIK), which allows for a different kind of API. So it's mostly a matter of personal preferences now.
Sorry for the dumb questions, I haven't worked with js much, but what does this add to the standard library?
Going just by the one example there, I can see that timezone by location is probably extra, that's perfectly fine. The endOf modifier seems more like a helper to fix broken date/time model in js? But other parts should probably be in the standard library? I was under the impression js is improving fast nowadays, will stuff like this get into the language?
The standard library is really difficult to work with and does very little for you. There are some improvements to Dates coming through the standards pipeline but it will be a while.
I think you might be downvoted because you plastered a link with no explanation. It doesn't actually address my concern, which was that when I was reading the documentation I did not see any examples.
I develop Java/kotlin as well, so I'm used to the API. It feels verbose, but it makes a lot of sense. Date-handling is complex, a library shouldn't hide that.
That's not what I meant by "works". I meant works for the use case. Moment has worked 100% for me whenever I used it. It's API is simple and does what it says.
https://moment.github.io/luxon/docs/manual/faq/moment.html
Major differences: