Need a special offer?Find out if your project fits.
+

Is there support for "week" on dates in JSON?

Answered
Projet TCD asked on February 24, 2020

Hello,
I would like to know if there is any way to represent the week in the date natively when using JSON data as row or column?
Either as:

  • New types like "weekOfYear" (equivalent to Excel WEEKNUM for week starting on Sunday or ISOWEEKNUM for week starting on monday) and "weekOfMonth" (based on one or the other value)
  • Hierarchy year/quarter/month/week/day

Thank you very much
Best regards,

11 answers

Public
Illia Yatsyshyn Illia Yatsyshyn Flexmonster February 24, 2020

Hello,
 
Thank you for contacting us.
 
We would like to kindly inform you that such an aggregation is not included is the list of data types supported by Flexmonster by default. We recommend preprocessing the data set so it contains all required fields including the week number and present it as multilevel hierarchy.
 
You are welcome to check out an example we have prepared in order to demonstrate the mentioned approach.
 
We hope it works for you.
 
Best regards,
Illia

Public
Projet TCD February 28, 2020

Hello Illia,
 
Thank you very much for your answer, we will be using your proposed approach.

Public
Mantas May 31, 2022

Hello, I have same situation - I need week. But example does not work - it shows only year without structure. Did I miss something?

Public
Maksym Diachenko Maksym Diachenko Flexmonster May 31, 2022

Hi, Mantas!

Thank you for your question.

Please note that currently, we do not have the functionality for working with weeks of dates. Therefore, our team recommends preprocessing data the same way as described above.

The example from the previous reply is using the deprecated approach for creating multilevel hierarchies. Now, you only have to set:

  • hierarchy property - the name of the hierarchy.
  • parent property - has to contain the `uniqueName` of parent hierarchy member.

We have modified the sample using the new technique for creating hierarchical members. You are welcome to test it: https://jsfiddle.net/flexmonster/e8q3pz9c/.

Please let us know if this approach works for you.

Best Regards,
Maksym

Public
Maksym Diachenko Maksym Diachenko Flexmonster June 7, 2022

Hi, Mantas!

Our team is wondering whether you tried using the provided approach for working with weeks. Please tell us whether the described solution works for you.

Best Regards,
Maksym

Public
Mantas June 8, 2022

Hello, yes. I generated weeks server side. But problem is if week splits between years. Then it becomes week 53, and goes to the end, but it should be before 1.

Public
Maksym Diachenko Maksym Diachenko Flexmonster June 8, 2022

Hi, Mantas!

Thank you for your reply. 

Please check the JavaScript function for calculating the date's week from the following StackOverflow thread. This function treats dates at the beginning of the new year as a separate week if the new year starts in the middle of a week. It returns week numbers starting from 0.

/**
 * Returns the week number for this date. dowOffset is the day of the week the week
 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
 * the week returned is the ISO 8601 week number.
 * @param int dowOffset
 * @return int
 */

Date.prototype.getWeek = function (dowOffset) {

    dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() - 
    (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;

    //if the year starts before the middle of a week
    if(day < 4) {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if(weeknum > 52) {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
              the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    }
    else {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

If you are not using JavaScript on your server, this function can still help you understand the general idea of an algorithm.

Hope you will find this function helpful for server-side week numbers generation.

Best Regards,
Maksym

Public
Mantas June 13, 2022

hello, problem is not the week number - I have correct number. Problem is component - it sorts directly by week number, and not includes year. So, instead of timeline in line chart:
  2021-53, 2022-1, 2022-2, ...
I get:
  2022-1,2022-2, ..., 2021-53
 
I format structure like this:
{
  "DateWY": { "caption": "Metai", "type": "number", "hierarchy": "Metai sav.", "level": "Metai" },
  "DateW": { "caption": "Savaitė", "type": "number", "hierarchy": "Metai sav.", "level": "Savaitė", "parent": "DateWY" }
}
 
Values:
[
  {"DateWY": 2021, "DateW": 53},
  {"DateWY": 2022, "DateW": 1},
  {"DateWY": 2022, "DateW": 2},
...
]

Public
Maksym Diachenko Maksym Diachenko Flexmonster June 14, 2022

Hi, Matias!

Thank you for explaining the issue.

We were unable to reproduce similar sorting behavior. Please check the following JSFiddle code snippet.
Our team would be very grateful if you could modify the sample so that the problem is reproducible.

Looking forward to hearing from you.

Best Regards,
Maksym

Public
Mantas June 14, 2022

I see I can not reproduce it anymore too - it can be I used old component. Sorry for disturbance, it works fine now.

Public
Maksym Diachenko Maksym Diachenko Flexmonster June 14, 2022

Hi, Mantas!

We are glad to hear that the provided solution for dates with weeks works for you.
Feel free to contact us in case more questions arise.

Best Regards,
Maksym

Please login or Register to Submit Answer