Date Formats

You can set the default date format from within the content type’s ViewModel. When resolving the date format to use, Brightspot checks for this configuration if there is no date format specified in a theme’s properties (as described in Date Format) or in a site’s settings (as described in Date Format).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package brightspot.core.article;

import brightspot.view.core.article.ArticlePageView;
import brightspot.core.tool.DateTimeUtils;
import brightspot.core.page.CurrentPageViewModel;
import com.psddev.cms.view.PageEntryView;
import com.psddev.cms.view.ViewModel;

public class ArticlePageViewModel extends ViewModel<Article> implements ArticlePageView, PageEntryView {

    @CurrentPageViewModel
    protected PageViewModel page;

    private static final String DATE_FORMAT_KEY = "dateFormat";
    private static final String DEFAULT_DATE_FORMAT = "MMMM d, yyyy";

    @Override
    public CharSequence getDatePublished() {
        return DateTimeUtils.format(model.getPublishDate(), ArticlePageView.class, DATE_FORMAT_KEY, page.getSite(), DEFAULT_DATE_FORMAT);
    }

    /* Other Getters */
}

In the previous snippet—

  • Lines 11–12 declare a variable page and assign it the default value PageViewModel.class.
  • Line 15 declares a date format string.
  • Line 19 retrieves the Model’s publication date and outputs the date using the configured format string.

See also: