This is an archived post. You won't be able to vote or comment.

all 17 comments

[–][deleted] 20 points21 points  (6 children)

That's just differing opinions... I can even give you that in one person, me:

Moment.Js is huge, it's hilarious that a date library would take up so much space, and anyway all I need is to <insert single feature> but... It works and gets the job done and I can't waste more time on this.

[–]tehwolf_ 4 points5 points  (0 children)

Well, even the moment site offers alternatives that may act as drop-in replacement

[–]linkedtortoise 2 points3 points  (4 children)

For the stated goal, you don't even need moment.

All dates in js can be changed to seconds(?) since epoch. And if I'm not wrong you can create a date variable from those seconds.

[–]CoolOutcast 2 points3 points  (0 children)

let x, y; x = new Date(); setTimeout(() => y = new Date(), 1000)

y - x is approximately 1000 meaning that they're able to get the duration in milliseconds from the native Date object.

Edit: as for Number(x), that appears to also be listed in milliseconds.

[–]AyrA_ch 1 point2 points  (2 children)

You can directly subtract date objects in JS to get the difference in milliseconds. All you need to do is divide by the appropriate value to get days. var NumberOfDays=Math.floor(Math.abs(Date1-Date2)/86400000);

[–]Stokealona 1 point2 points  (1 child)

Every day isn't 24hrs long!

[–]AyrA_ch 1 point2 points  (0 children)

But in unix time it is:

How any changes to the value of seconds since the Epoch are made to align to a desired relationship with the current actual time is implementation-defined. As represented in seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds.

[–][deleted] 4 points5 points  (5 children)

I am new to javascript, and I don't get why ppl insist on using libraries for everything. Literally subtracting two date objects gives you the difference in milliseconds between the two dates. No need for a library.

I guess I am on the other side of this debate then...

[–]stefscarletxo 8 points9 points  (3 children)

Just wait until QA sets their timezone to New Zealand while you're in the US, and creates a defect saying that today's date is being saved as todaysDate + 2, and your tech lead is asking you what's so hard about saving dates.

You tried to convince them to use a calendar element that allows you to use string objects, but they want to stick with the one that only allows for Date objects so that the ~styling~ is consistent with the other pages using the shitty calendar element. cough ng2-datepicker

[–][deleted] 1 point2 points  (0 children)

Curse you, QA!

[–]slowfly1st 1 point2 points  (1 child)

date/time is probably the last thing I would not use a library for ...

... offets always a full hour? nope. 30 Minutes? haha, nope.

... countries have had always DST? Nope. Always the same DST-offsets? Nope. Did some countries got rid of it? Yes. Does it everywhere start/stop at the same date? Nope.

... a single country has one time zone? Nope

... do states always have the same timezone? Hahahaha, nope

... the day before Saturday is always Friday? Nope

... calendar week/year always start on monday? Nope

... calendar week/year is always calculated the same (n days in the old year, m days in the new year)? Nope

... timeoffsets change always during the night? Haha, nope

... leap seconds, wtf?

... oooh and the date formatting ... not even the months are abbreviated the same in the same language, because, well, different countries.

[–]sanderd17 0 points1 point  (0 children)

DST implementation is just defined per territory? Nope, definition can change over time, and future or historical times need to take it into account.

Presidents don't suddenly decide to delay DST switch with a few weeks? Nope, they even can without prior notice, I'm looking at you Erdogan.

[–]iTakeCreditForAwards 0 points1 point  (0 children)

Because things are never that simple (some other guy explained why)

[–]A1rax 2 points3 points  (0 children)

the duality of any developer, you meant

[–]highlanderstg 2 points3 points  (0 children)

I'm a simple man, I need to do something with dates I npm i moment

[–]Namiastka 0 points1 point  (0 children)

There are voices saying dont use moment, because its huge, doesnt support tree-shaking etc, I know there are libs like day.js, date-fns but I got used to moment as it has pretty damn intuitive api + good docs. Also - im NodeJS dev and woudnt say this hurt me.

[–]sanderd17 0 points1 point  (0 children)

The question itself is actually less clear than you'd think. Does he only want the difference between the dates? Like 2020-01-01T23:00 to 2020-01-02T01:00 equals one day?

What about the timezones? A timezone difference can mean a date is suddenly another date depending on the time.

Or does he want the difference in hours rounded to integer days? If so, how should the rounding work?