DateFormate

UI Components for React
Post Reply
Ramprit
Posts: 2
Joined: 26 Aug 2017, 14:01

19 Jan 2019, 16:01

How to set costume date format in DataTable.

merve7
Posts: 861
Joined: 12 Sep 2017, 10:44

01 Feb 2019, 13:53

What do you want to use date format for? For example filtering, title or row? Please, could you explain more?

Agustin Caamaño
Posts: 7
Joined: 14 Dec 2021, 17:54

14 Dec 2021, 18:33

You can use this code, this will change the format of a Date object

Code: Select all

	dateTemplate(rowData, field) {
        	return rowData[field.field].toLocaleDateString(navigator.language, {
           		day: '2-digit',
            		month: '2-digit',
            		year: 'numeric',
        	})

    	}

    	dateTimeTemplate(rowData, field) {
       	 	return rowData[field.field].toLocaleDateString(navigator.language, {
            		day: '2-digit',
            		month: '2-digit',
            		year: 'numeric',
            		hour: '2-digit',
            		minute: '2-digit'
        	})
    	}
You will have to call the functions from the Column component at the body property like this:

Code: Select all

<Column
 	key={item.field}
 	field={item.field}
 	header={item.header}
	body={item.datatype == "date" ? this.dateTemplate : this.dateTimeTemplate}
  	sortable
	filter
 	filterType="date"
	filterPlaceholder={item.filterPlaceholder}
 	filterField={item.field}
 	sortField={item.field}
	style={{width: item.header.length + 1 + "rem"}}
 	dataType="date"
	filterElement={this.dateFilterTemplate}
/>

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 6 guests