mdehoog / Semantic-UI-Calendar

Calendar module for Semantic UI
MIT License
805 stars 127 forks source link

Format date yyyy/MM/dd #81

Open nestorvanz opened 7 years ago

nestorvanz commented 7 years ago

Do yo have an implementation for a format output date into input field?

I'm looking for a property to set the format date. Something like this:

{
  formatDate: 'yyyy/MM/dd'
}
uniquejava commented 7 years ago

@nestorvanz , I do it like this.

var calendarOpts = {
    type: 'date',
    formatter: {
        date: function (date, settings) {
            if (!date) return '';
            var day = date.getDate() + '';
            if (day.length < 2) {
                day = '0' + day;
            }
            var month = (date.getMonth() + 1) + '';
            if (month.length < 2) {
                month = '0' + month;
            }
            var year = date.getFullYear();
            return year + '/' + month + '/' + day;
        }
    }
};
$('.ui.calendar').calendar(calendarOpts);
dayze commented 7 years ago

If you can use Moment.js, you can do this

formatter: {
  date: function (date, settings) {
    let momentDate = new Moment(date)
    return momentDate.format('yyyy/MM/dd')
  }
}

https://momentjs.com/docs/#/displaying/