The ISO 8601 definition for week 01 is the week with the first Thursday of the Gregorian year
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in W01. If it is on a Friday, it is part of W53 of the previous year. If it is on a Saturday, it is part of the last week of the previous year which is numbered W52 in a common year and W53 in a leap year. If it is on a Sunday, it is part of W52 of the previous year.
https://en.wikipedia.org/wiki/ISO_week_date
What is the first day of the week?
We are going to create an app that shows us the days of the first week of each year, for this we are going to use ChatGPT , we will ask:
(Adapted to App Inventor)
semana.htm
<script>
function getWeekDays(year, week) {
const startDate = getWeekStartDate(year, week);
const weekDays = [];
for (let i = 0; i < 7; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
weekDays.push(currentDate.toDateString());
}
return weekDays;
}
function getWeekStartDate(year, week) {
const jan1 = new Date(year, 0, 1);
const jan1DayOfWeek = jan1.getDay();
let startDate;
if (jan1DayOfWeek <= 4) {
startDate = new Date(year, 0, 1 - jan1DayOfWeek + 1);
} else {
startDate = new Date(year, 0, 1 + (7 - jan1DayOfWeek) + 1);
}
startDate.setDate(startDate.getDate() + (week - 1) * 7);
return startDate;
}
datos = window.AppInventor.getWebViewString().split(","); // Entrada de datos.
var year = datos[0];
var week = datos[1];
const weekDays = getWeekDays(year, week);
weekDays.forEach(day => console.log(day));
const weekDaysJSON = JSON.stringify(weekDays);
window.AppInventor.setWebViewString("" + weekDaysJSON);
</script>
p100_Semana_JS.aia (3.9 KB)
3 Likes
2.- Day of the week of 31 December.
From: https://en.wikipedia.org/wiki/ISO_week_date
Did you know... these symbols mean floor and ceil?
p100_Semana_JS_v2.aia (4.5 KB)
Remember that in the ISO8601 standard the weeks start on Monday.
1 Like
Juan_Antonio:
App
consulta
como puedo calcular o obtener la cantidad de semanas de un año
como por ejemplo
2023 tiene 52
2020 tiene 53
saludos
I showed you in your topic....
3.- Number of weeks in a year according to ISO8601.
p100_Semana_JS_v3.aia (5.3 KB)
Label2 is last day of the year. (1: Monday - ........... - 0: Sunday)
1 Like
4.- Format date: DD MM YYYY
semana.htm
<script>
function getWeekDays(year, week) {
const startDate = getWeekStartDate(year, week);
const weekDays = [];
for (let i = 0; i < 7; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
const formattedDate = formatDate(currentDate);
weekDays.push(formattedDate);
}
return weekDays;
}
function formatDate(date) {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day} ${month} ${year}`;
}
function getWeekStartDate(year, week) {
const jan1 = new Date(year, 0, 1);
const jan1DayOfWeek = jan1.getDay();
let startDate;
if (jan1DayOfWeek <= 4) {
startDate = new Date(year, 0, 1 - jan1DayOfWeek + 1);
} else {
startDate = new Date(year, 0, 1 + (7 - jan1DayOfWeek) + 1);
}
startDate.setDate(startDate.getDate() + (week - 1) * 7);
return startDate;
}
datos = window.AppInventor.getWebViewString().split(","); // Entrada de datos.
var year = datos[0];
var week = datos[1];
const weekDays = getWeekDays(year, week);
weekDays.forEach(day => console.log(day));
const weekDaysJSON = JSON.stringify(weekDays);
window.AppInventor.setWebViewString("" + weekDaysJSON);
</script>
2 Likes