4

How can I convert the date below into this template (dd/mm/yyyy hh:mm:ss) ?

05/04/2021 14:52

I tried to do it that way, but I only get the time and not the date with time.

var data = new Date('05/04/2021 14:52');
var time = data.toLocaleTimeString('pt-PT', {hour12: false});
console.log(time);
Isaac
  • 150
  • 1
  • 1
  • 12
  • Use `toLocalDateString` to get the date. Then concatenate them. – Barmar Apr 05 '21 at 17:58
  • I did it like this var time = data.toLocaleDateString ('pt-PT'); but the date returned, but the time did not come – Isaac Apr 05 '21 at 18:01
  • this will achieve as you want. tested console.log(data.toLocaleString('pt-PT',{hour12: false})); @Isaac – Ashish Sharma Apr 05 '21 at 18:04
  • use moment js. moment(new Date(dateString)).format('DD/MM/yyyy HH:mm:ss') – Nonik Apr 05 '21 at 18:05
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Heretic Monkey Apr 05 '21 at 18:10
  • @ashish I did it like that, but the day changed places with the month – Isaac Apr 05 '21 at 18:10
  • @Nonik here worked with moment.js! thanks – Isaac Apr 05 '21 at 18:11
  • Note that using `new Date(string)` with a string in a format other than certain formulations of ISO 8601 may not work across browsers or platforms. See https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results – Heretic Monkey Apr 05 '21 at 18:12

3 Answers3

3

This is my solution. If you want to create a advanced format, you can read more about object Intl https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl

const formatDate = new Intl.DateTimeFormat("en" , {
  day: "2-digit",
  month: "2-digit",
  year: "numeric",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
  hour12: false
});

console.log(formatDate.format(new Date('05/04/2021 14:52')))
Dai Nguyen
  • 66
  • 6
  • If you see a comment that starts "Does this answer your question?" followed by a link to a question, follow that link and look at the answers; if they do in fact answer the question, please refrain from answering the question. We want to keep the best answers to similar questions in one place. – Heretic Monkey Apr 05 '21 at 18:42
  • Ok I understood. – Dai Nguyen Apr 06 '21 at 00:12
2

You can use below script

var data = new Date('05/04/2021 14:52');
console.log(data.toLocaleString('en-GB',{hour12: false}));

Output : "04/05/2021, 14:52:00"

Ashish Sharma
  • 446
  • 5
  • 14
2

If you need more date-related stuff than simple date formatting, you can use Moment.js.

moment().format('MMMM Do yyyy, h:mm:ss a'); // April 5th 2021, 9:16:13 pm
moment().format('DD/MM/yyyy hh:mm'); // 05/04/2021 21:18

If you need to format your Date object, simply use:

moment(date).format('DD/MM/yyyy hh:mm');

Moment.js is also useful for operation on dates like days, weeks, months adding/subtracting, getting the start of a week, month, quarter, and many other useful operations.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
OLezhko
  • 150
  • 3