Comments (8)
Glad you enjoy it! By week number do you mean the week of the year? So Jan 1 being 1, Jan 10 be 2?
from tempo.
Exactly!
I tried to see if I could create something but it looks more challenging then I expected.
The closest I got so far was as following:
function weekNumber(givenDate: Date, startOfWeek: number) {
const millisecondsInWeek = 604800000;
const startDate = weekStart(givenDate, startOfWeek);
const startOfYear = yearStart(startDate);
const diff = startDate.getTime() - startOfYear.getTime();
return Math.round(diff / millisecondsInWeek) + 1;
}
But this still fails for dates which weekStart
end up in a previous year.
For example: getWeekNumber(new Date(2013, 0, 0), 1)
gives me week 53
from tempo.
@DJWassink I think you actually have it about right except for new Date(2013, 0, 0)
This is actually December 31 while argument 2 is month index, argument three is "day" with a starting number of 1
not 0
so 0
is actually Dec 31! Ha! Thanks JavaScript 🪄
from tempo.
Ooh wow yeah 🤦♂️
However the bug still persist for other dates where the start of week is in the previous year.
A more obvious one might be 31 dec 2013. new Date(2013, 11, 31)
.
Which my code currently reports as week 53 but should be week 1.
I simply test with a big loop that goes through every day starting in 2013 till now and compare the moment.js week versus mine.
from tempo.
Hmmm... wait — how could 2013-12-31 be in week 1? Are we saying that week one of 2024 was (assuming sunday is the first day of week) would be Dec 31 - Sat 6th even though it spans the year gap? Or is week one the 7th - 13th I guess there is some subjectivity and intuition there that would need to be nailed down.
from tempo.
As far as I'm familiar with calendars the first week of 2024 would be Dec 31 - 6th of jan (on a sunday calendar).
For example google calendar for me starts on a monday (dutch) and goes from 1 till 7 jan.
More interesting 2023 for example has 2 - 8 jan as week 1 (assuming monday).
from tempo.
Yeah, I suppose I haven’t thought much about it honestly I guess that does make sense. I wouldn’t wake up on the 7th of January and think to myself, "ok lets get this first week of the year underway". Ok! I think we could get this into tempo for sure.
from tempo.
Also I think this must be relative to weekStartsOn
, date-fns like logic link, because someone have start on monday, some on sunday, some on saturday 🙂
from tempo.
Related Issues (20)
- Bugfix deviceTZ undefined case error HOT 3
- Unnecessary code in bundles HOT 5
- Are there any plans to support timezone formats like +-HH:mm in the parse function?
- Feature Request: Difference between datetimes HOT 1
- format YYYY-MM returns YYYY-M HOT 3
- yearStart and yearEnd may need to be added to types - v0.1.15 HOT 2
- Feature request: `differenceInMilliseconds` / `differenceInSeconds` / `differenceInMinutes` / etc. HOT 1
- FeatureReuet: Support for timezone format +-HH:mm in format token. HOT 1
- Filtering out year still includes comma HOT 2
- Invalid offset error for +0000 HOT 2
- Optional argument in `is*` functions HOT 3
- Format with `a` or `A` are different locally vs CI HOT 2
- When user timezone is `Europe/Lisbon`, the tempo's `range` function throws an "Invalid offset" exception. HOT 6
- Feature Request : Duration format HOT 1
- Error: Invalid time zone specified: Etc/Unknown HOT 1
- Override the default timezone HOT 2
- "diffMinutes does not exist" error HOT 3
- Old timezones require second precision HOT 1
- Issue : format function throw error when locale passed including numbering system ( ar-u-nu-arab )
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tempo.