In data1[0].date is the date / time: 2017-10-10 12:10:44 How to display only the date: 2017-10-10 ?
|
1 answer
I give:
Date.prototype.format = function(format = 'yyyy-mm-dd') { if (this.toString() === 'Invalid Date') { return; } const replaces = { yyyy: this.getFullYear(), mm: ('0' + (this.getMonth() + 1)).slice(-2), dd: ('0' + this.getDate()).slice(-2), hh: ('0' + this.getHours()).slice(-2), MM: ('0' + this.getMinutes()).slice(-2), ss: ('0' + this.getSeconds()).slice(-2) }; let result = format; for (const replace in replaces) { result = result.replace(replace, replaces[replace]); } return result; }; console.log(new Date().format()) console.log(new Date().format('dd/mm/yyyy hh.MM.ss')) Although in this case, you can do this:
const date = '2017-10-10 12:10:44'; console.log(date.split(' ')[0]); - Thank. The second option is more convenient - Valery Orlov
- Can you still give the correct answer to a person? And then he will be with the date, how to work with the string ... - Stepan Kasyanenko
- @StepanKasyanenko so he has it and so the line. Meaning to parse it on the date and then collect the month and day back from it if you can just cut it off? If you know some more correct answer - give it yourself - Darth
- I think @ValeryOrlov liked this answer and God bless him. Did he learn something from him? Work with a date? No I do not think so. - Stepan Kasyanenko
|