Skip to content Skip to sidebar Skip to footer

How To Format The Date To (dd/mm/yyyy Hh:mm:ss)

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.

Solution 1:

You can use below script

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

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

Solution 2:

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

moment().format('MMMM Do yyyy, h:mm:ss a'); // April 5th 2021, 9:16:13 pmmoment().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, week, month adding/subtracting, getting the start of a week, month, quarter, and many other useful operations.

Solution 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 = newIntl.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(newDate('05/04/2021 14:52')))

Solution 4:

to get current formatted date dd/mm/yyyy in JavaScript.

there are different methods to consider,

Date.getDate()

This method returns the day of the month (from 1 to 31) for the defined date.

Date.getFullYear()

This method returns the year (four digits for dates between year 1000 and 9999) of the defined date.

Date.getMonth()

This method returns the month (from 0 to 11) for the defined date, based on to local time.

Post a Comment for "How To Format The Date To (dd/mm/yyyy Hh:mm:ss)"