Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return dd-mm-yyyy from Date() object

Desired return value should be a string formatted as dd-mm-yyyy.

Im trying to give a format date dd-mm-yyyy to ISOString and adding GMT but the code gives me this format. How can i do?

new Date().toISOString()
    .replace(/T/, ' ').      // replace T with a space
    .replace(/\..+/, '');     // delete the dot and everything after

'2012-11-04 14:55:45'

like image 862
J.arc Avatar asked Mar 07 '16 11:03

J.arc


People also ask

How do I convert a date object to date?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

How do I convert a date object to number?

Example: Convert Date to Number The new Date() gives the current date and time. To convert the name to a number, we use the getTime() method. The getTime() method returns the number of milliseconds from midnight of January 1, 1970 (EcmaScript epoch) to the specified date.


1 Answers

For your example '2012-11-04 14:55:45'

You can do: new Date('2012-11-04 14:55:45').toISOString().split('T')[0] in a single line :)

like image 62
Filipe Andrade Avatar answered Oct 12 '22 10:10

Filipe Andrade