6 ms·
In one project at a previous company, it took three people more than a week to try to get a third party date picker to work with the application. The funny thin
by hsn915 4y ago
In one project at a previous company, it took three people more than a week to try to get a third party date picker to work with the application. The funny thing is all the time was spent in writing glue code and debugging that glue code. We had more glue code than we would have code if we had just implemented the date picker internally.
Date Picker is one of those things that seem like a big complicated thing but is actually very very very simple to implement.
Honest advice: implement one yourself and use it in your projects. It doesn't even take 600 lines (even when you include the css).
The "hardest" part is displaying a calendar month (for a given year/month combination), in a grid layout, where columns represent week days. But that is actually not hard at all, because the builtin Date class has all the building blocks necessary to make this work. Even the i18n can be done with the builtin Date class.
Given a (year, month) combination, where month is specified from 1-12, this is how you get the weekday of the first day in the month:
let firstWeekDay = new Date(year, month - 1).getDay()
This will be a number from 0 to 6, where 0 is sunday.
The other bit of information you need is the last day of the month, which can be obtained thus:
let lastday = new Date(year, month, 0).getDate()
Now just loop over the range 1...lastday and fill in the grid of the days of the week.
It's that simple.
- PeterisP 4y agoIt's never that simple. For starters, for half the world you need to show Monday as the first day of the week, not Sunday.
- iamevn 4y agoThis is why I would just stick with <input type="date"> and let the user agent deal with that complexity
- Izkata 4y agoSoooo how many libraries remember to do it? And how many people remember to check the library they chose does it?
- marcosdumay 4y agoYeah... For some applications that is very important, for others nobody cares. YMMV. About 2/3 of the people on this planet do not agree with the west about when the year starts, what are the months, and things like that. This may be a huge roadblock for you, or just ignoring it may be perfectly fine.
- anomaloustho 4y agoHmm, it looks like most countries (incl China) adopt the Gregorian calendar as their official calendar. India and much of Northern Africa do not adopt the Gregorian calendar. By my estimate, that would be 1/5 of the population as opposed to 2/3.
- marcosdumay 4y agoHum... It seems things are both better and worse than I expected. Most of the world officially adopted the Gregorian calendar (one of the regions you missed, the Middle East is on the process of changing, with some countries further than others), but most of those countries have a dual calendar system, so depending on what you do, you may need one or the other. Yes, for counting population, China is what weights the most, and they almost completely moved already.
- avgcorrection 4y agoIt’s very annoying when I pick the left-most column because I assume it’s Monday only to realize that it’s in fact Sunday. I’ve never even heard Americans use the concept, like saying “see you at the start of the week (implied Sunday)”. They do talk about the work week and weekend, though. But maybe the should talk about the week start (Sunday), work week and weekend (only Saturday, maybe Friday evening). It’s partly my own fault for using the English version of software since I don’t want to use localized GUI navigation guides (they might not exist).
- hsn915 4y agoHow hard do you think it would be to shift the day of the week? It's just a + and a % operation. How does this justify installing a 500kb javascript dependency? Saying this is "not that simple" is just you mentally deciding to lose for no reason.