Hello everyone, I am trying to format a relative d...
# gd-beginners
j
Hello everyone, I am trying to format a relative date filter, i.e past 6 months, using the method seen here:
formatRelativeDateRange
. However, I'm unsure if this is the right way to go about it as there is an expected 4th argument called
translator.
After walking through the source code, I'm unsure of how to use this method. Is there an easier way to format a relative date to something like YYYY-MM-DD so I can populate a datepicker on page load? Thank you!
Copy code
const contextFilters = DashboardStoreAccessorRepository.getDashboardSelectForDashboard(report.id)(
  selectFilterContextFilters,
);

const { dateFilter } = contextFilters.find((filter) => filter.dateFilter);
        
console.log(DateFilterHelpers.formatRelativeDateRange(<http://dateFilter.to|dateFilter.to>, dateFilter.from, dateFilter.granularity));
m
Hi Jared, The method you are using is currently in Beta and thus discouraged to use in a production environment. More information about the
translator
can be found here (for development purposes): https://sdk.gooddata.com/gooddata-ui-apidocs/v8.12.0/docs/sdk-ui-filters.idateandmessagetranslator.html#__docusaurus However, I think a more straightforward way would be to follow the instructions mentioned here: https://sdk.gooddata.com/gooddata-ui/docs/date_filter_component.html You can also try out interactive code samples with
date filter component
in our sandbox. Please let me know if this helps!
j
@Marek Horvat thank you for the info. We are doing a custom UI, so my goal is to take a relative date and transform it into something like "5/30/2023" for our react-datepicker component. So if our relative date filter returns something like last 6 months, what is the best way to go about this?
I was able to get this method to return something, but like you mentioned...it is in beta:
Copy code
console.log(DateFilterHelpers.formatRelativeDateRange(dateFilter.from, <http://dateFilter.to|dateFilter.to>, dateFilter.granularity, intl));
returns "Last 6 months"
I suppose I could get hacky and build up on my own date based off of the
n
value but that seems like a bad way to approach this
m
Hi Jared, It seems that there is no out-of-the-box solution for this. You might, however, leverage the date-fns library to create an interface to translate the strings shown on front end. Alternatively, you can find the definition of translators in this repo: https://github.com/gooddata/gooddata-ui-sdk/blob/master/libs/sdk-ui-filters/src/DateFilter/utils/Translations/Translators.ts and work from there.
1