Hi,
I'm creating a function that should generate a week number.
I can't draw it, where am I making a mistake, please?
Hi,
I'm creating a function that should generate a week number.
I can't draw it, where am I making a mistake, please?
This is my original code that I wanted to change:
Hmm, that's not what I saw in your OP before. Maybe it was edited by accident?
I saw
var currWeek = getCurrentWeekNumber;
But that explains…
Okay, so define the string for week number in your resources, very similar to the above example:
resource-eng:
<strings>
<string id="WeekNumber">WeekDay</string>
</strings…
No problem!
I will say that "WeekDay" is probably not the correct string in English. "Week day" means day of the week (Monday, Tuesday, etc.)
In this case I would go with "Week". (On…
No, I would use the function linked above (https://forums.garmin.com/developer/connect-iq/f/discussion/2778/week-of-year/27615#27615). as follows:
function iso_week_number(year, month, day)
{
//... see linked post
}
function your_code(){
var info = Gregorian.info(Time.now(), Time.FORMAT_SHORT);
var weekNumber = iso_week_number(info.year, info.month, info.day);
// ...
}
Okay, I'll take some advice.
Thank you very much.
function getWeekNumber(moment) { var dateInfo = Greg.info(moment, Time.FORMAT_SHORT); var firstDay = Greg.moment({:year => dateInfo.year, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0}); var firstDayDoW = Greg.info(firstDay, Time.FORMAT_SHORT).day_of_week; firstDayDoW = firstDayDoW == 1 ? 8 : firstDayDoW; var firstTuesday = firstDay.value() + (firstDayDoW > 5 ? (4 + 8 - firstDayDoW) : (5 - firstDayDoW)) * 86400; var thisTuesday = moment.value() + (5 - (dateInfo.day_of_week == 1 ? 8 : dateInfo.day_of_week)) * 86400; var currentWeek = ((thisTuesday - firstTuesday) / 604800f + 1.5f).toNumber(); return currentWeek == 0 ? getWeekNumber(Greg.moment({:year => dateInfo.year - 1, :month => 12, :day => 31, :hour => 0, :minute => 0, :second => 0})) : currentWeek; }
So my suggestion was to use the code from this post to calculate the ISO week number: iso_week_number() (and julian_day())
The function is already written for you, you don't have to write it again.
And how do i do dc.draw?
Just do the same thing you did before. The only thing that's changed is how the week number is calculated (and how you're getting today's date.) (There's no need to add the timezone offset -- Gregorian.info() already gives you information in local time, as compared to Gregorian.utcInfo() which gives you info in UTC time.)
var today_info = Gregorian.info(Time.now(), Time.FORMAT_SHORT); var currWeek = iso_week_number(today_info.year, today_info.month, today_info.day); dc.setColor(Graphics.COLOR_GREEN,Graphics.COLOR_BLACK); dc.drawText(x, y, $.fontTest, currWeek, Graphics.TEXT_JUSTIFY_CENTER);
function getWeekNumber(moment) { var x = 120, y = 105; var dateInfo = Gregorian.info(moment, Time.FORMAT_SHORT); var firstDay = Gregorian.moment({:year => dateInfo.year, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0}); var firstDayDoW = Gregorian.info(firstDay, Time.FORMAT_SHORT).day_of_week; firstDayDoW = firstDayDoW == 1 ? 8 : firstDayDoW; var firstTuesday = firstDay.value() + (firstDayDoW > 5 ? (4 + 8 - firstDayDoW) : (5 - firstDayDoW)) * 86400; var thisTuesday = moment.value() + (5 - (dateInfo.day_of_week == 1 ? 8 : dateInfo.day_of_week)) * 86400; var today_info = Gregorian.info(Time.now(), Time.FORMAT_SHORT); var currentWeek = iso_week_number(today_info.year, today_info.month, today_info.day); dc.setColor(Graphics.COLOR_GREEN,Graphics.COLOR_BLACK); dc.drawText(x, y, $.fontTest, currentWeek, Graphics.TEXT_JUSTIFY_CENTER); return currentWeek == 0 ? getWeekNumber(Gregorian.moment({:year => dateInfo.year - 1, :month => 12, :day => 31, :hour => 0, :minute => 0, :second => 0})) : currentWeek;
Problém:
Undefined symbol "iso_week_number" detected.
Undefined symbol "iso_week_number" detected.
If you click on this post (https://forums.garmin.com/developer/connect-iq/f/discussion/2778/week-of-year/27615#27615), you will see 3 functions called julian_day(), is_leap_year() and iso_week_number() (this function is pretty long).
Copy all the functions into your app so you can use them.
Honestly, I don't understand your code above. It looks like you still have a bunch of code to try to calculate the week number, even though you are also using the function from the other post. You would only need to use one or the other, not both.
Your original getWeekNumber() function (which you have now deleted from your original post), took dc as an argument, but now it takes moment? It calls itself recursively, and it also returns the week number? Is the purpose of the function to draw the week number or to get the week number? To avoid confusion, it should do one or the other, but not both. And the name should reflect what it does.
I mean I know it's called "get" week number, but originally it was drawing the week number. I don't understand the full context of how this new function is supposed to be called.
I'll go back to your *original* code and add my suggestion. Plus I will rename the function to drawWeekNumber() so it's less confusing.
// Copy julian_day(), is_leap_year() and iso_week_number() from https://forums.garmin.com/developer/connect-iq/f/discussion/2778/week-of-year/27615#27615 function drawWeekNumber(dc) { var x = 120, y = 105; var today_info = Gregorian.info(Time.now(), Time.FORMAT_SHORT); var currentWeek = iso_week_number(today_info.year, today_info.month, today_info.day); dc.setColor(Graphics.COLOR_GREEN,Graphics.COLOR_BLACK); dc.drawText(x, y, $.fontTest, currentWeek, Graphics.TEXT_JUSTIFY_CENTER); } function onUpdate(dc) { // ... drawWeekNumber(dc); // ... }
Hello, I will try to explain everything.
First I created the code to display the current number of the week, but I didn't know how to draw dc.draw correctly.
You recommended me to use:
var currWeek = year_duration.value() / (3600*24) / 7 + 1; // year_duration.value() is in seconds
everything worked for me, but you wrote me that this method of solution is not very good and you recommended the solution using the link:
https://forums.garmin.com/developer/connect-iq/f/discussion/2778/week-of-year/27615#27615)
I copied all the code into my function, including the function:
function getWeekNumber(moment) { var dateInfo = Greg.info(moment, Time.FORMAT_SHORT); var firstDay = Greg.moment({:year => dateInfo.year, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0}); var firstDayDoW = Greg.info(firstDay, Time.FORMAT_SHORT).day_of_week; firstDayDoW = firstDayDoW == 1 ? 8 : firstDayDoW; var firstTuesday = firstDay.value() + (firstDayDoW > 5 ? (4 + 8 - firstDayDoW) : (5 - firstDayDoW)) * 86400; var thisTuesday = moment.value() + (5 - (dateInfo.day_of_week == 1 ? 8 : dateInfo.day_of_week)) * 86400; var currentWeek = ((thisTuesday - firstTuesday) / 604800f + 1.5f).toNumber(); return currentWeek == 0 ? getWeekNumber(Greg.moment({:year => dateInfo.year - 1, :month => 12, :day => 31, :hour => 0, :minute => 0, :second => 0})) : currentWeek; }
This is my original code that I wanted to change:
function getCurrentWeekNumber(dc) { var x = 120, y = 105; var now = Gregorian.info(Time.now(), Time.FORMAT_SHORT); var year = now.year; var options_start = { :year => year, :month => 1, :day => 1, :hour => 0 }; var options_end = { :year => now.year, :month => now.month, :day => now.day, :hour => 0 }; var current_year_start = Gregorian.moment(options_start); var current_year_now = Gregorian.moment(options_end); var year_duration = current_year_now.subtract(current_year_start); var currWeek = year_duration.value() / (3600*24) / 7 + 1; dc.setColor(Graphics.COLOR_GREEN,Graphics.COLOR_TRANSPARENT); dc.drawText(x, y, $.fontTest, currWeek.format("%1d"), Graphics.TEXT_JUSTIFY_CENTER); return (((((year_duration.value()/60)/60)/24)/7)+1); }
I apologize for the complications,
I have already found a mistake and understood the principle. Now it works and the week number is displayed according to the recommended solution.
I still have one last question, how can I call a week number in another language using a string file?
I have language files in the application.
resources-"Lang" resources - "Lang" for Week ??? dc.drawText(x, y, $.fontTest, "Week "+currentWeek.format("%1d"), Graphics.TEXT_JUSTIFY_CENTER);
Thank you
Best regards
M.