Calculate the Week Number of the First Sunday of the Year?

How do I get the week number of the first Sunday of the year?

As you can see, I've been trying a lot of different techniques (using the one Display Text as a scratch pad/tester). I can get week numbers for a date in the future (and adjust them for Epoch, if needed), and I can get the information of a future date from today's date, but I can't figure out how to combine them to get the week number of the first Sunday of the year regardless of what today's date is.

Display Text.kmactions (5.3 KB)

PS Several just don't work, including the ones copied directly from the Wiki here Dates and Times [Keyboard Maestro Wiki]

The first week of the year (at least in the US) always starts on the first Sunday of the year.

So the answer will always be 1.

-Chris

1 Like

KM is marking the week containing Jan 1st as Week 1, thus my need to do this.

1 Like

The do things differently in Australia:

Week Numbers 2021 in Australia - Savvy Time

I don’t know how Peter is handling localizations...

-Chris

How do I get the week number of the first Sunday of the year?

Hi, @peternlewis . I know you're busy with the launch of 10, but could you please help me figure this out? I just need to a way to get KM to return the week number of the 1st Sunday of the year in my current locality.

Hey Pariah,

What is your locale?

And what are you wanting?

For the first Sunday in 2022 ICU date format returns week 2.

What year do you want? Any given year or this year?

-Chris

Find the date of the first Sunday, then use the ICU Week of Year field.

image

2 Likes

Date math is too complicated to not post the original macro...


Calculate the Week Number of the First Sunday of the Year v1.00.kmmacros (12 KB)

Macro-Image

Keyboard Maestro Export

2 Likes

Thank you @peternlewis and @ccstone . That's what I needed. I think my mistake was that I kept thinking I could do it with a single formula.

1 Like

You probably can - but sometimes its just not worth the effort figuring it out!

Hey Pariah,

Rule-of-the-thumb – never try to make a 1-liner (unless it makes really good sense to do so).

In general they are too difficult to compose, too difficult to read (one day, week, month, or year later), and too difficult to maintain.

Once you have something working you can think about making it more efficient. Sometimes that's highly desirable, because your first effort is clumsy and uneducated.

Sometimes better is the enemy of good enough.

I’m a perfectionist, so I frequently have to wrestle with my nature to avoid spending time unwisely.  :sunglasses:

-Chris

2 Likes