Hey,
Is it possible to show the current week number in a text element? Thanks!
You would have to look at ISO 8601.
Just do a search online and you will find many samples for you to look at and try to utilise.
https://www.epoch-calendar.com/support/getting_iso_week.html
https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
https://weeknumber.net/how-to/javascript
https://stackoverflow.com/questions/9045868/javascript-date-getweek
https://forum.sienn.com/default.aspx?g=posts&t=34
You could also look at using date-fns nodeJS if it supported in Wix.
https://date-fns.org/v2.0.0-alpha.25/docs/Getting-Started
I used this code:
Source: https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
function getWeekNumber(d) {
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
return [d.getUTCFullYear(), weekNo];
}
var result = getWeekNumber(new Date());
$w('#text45').text = ('Week ' + result[1]);
Thanks to @givemeawhisky
Thanks for posting your used code Quinten, hopefully it will help other users sometime in the future too.
For anyone who sees this thread and tried “Quinten Althues” code and realized that it didn’t work on your WIX site, i have modified his code to make it work with WIX as of today (10/06/2022).
Hope this will help your project!
//Calculate current week nr.
$w . onReady ( function () {
var currentDate = new Date ();
var startDate = new Date ( currentDate . getFullYear (), 0 , 1 );
var days = Math . floor (( currentDate . getTime () - startDate . getTime ()) / ( 24 * 60 * 60 * 1000 ));
console . log ( days )
var weekNumber = Math . ceil ( days / 7 );
console . log ( weekNumber )
})