Formatting dates using view models
You can set the default date format from within the content type’s view model. 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 formats and resource bundles) or in a site’s settings.
showLineNumbers {9,13,16}
1import brightspot.view.core.article.ArticlePageView;2import brightspot.core.tool.DateTimeUtils;3import brightspot.core.page.CurrentPageViewModel;4import com.psddev.cms.view.PageEntryView;5import com.psddev.cms.view.ViewModel;67public class ArticlePageViewModel extends ViewModel<Article> implements ArticlePageView, PageEntryView {89@CurrentPageViewModel10protected PageViewModel page;1112private static final String DATE_FORMAT_KEY = "dateFormat";13private static final String DEFAULT_DATE_FORMAT = "MMMM d, yyyy";1415@Override16public CharSequence getDatePublished() {17return DateTimeUtils.format(model.getPublishDate(), ArticlePageView.class, DATE_FORMAT_KEY, page.getSite(), DEFAULT_DATE_FORMAT);18}1920}
- 9. Declares a variable page, and the annotation assigns it the default value
PageViewModelclass. - 13. Declares a date format string.
- 16. Retrieves the model's publication date and outputs the date using the configured format string.
See also: