Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert JavaScript Date object to string that is compatible with datetime-local input?

Tags:

javascript

Basically I have a Date object. How can convert it to a string compatile with datetime-local format yyyy-MM-ddThh:mm?

I tried Date.toISOString method, but it doesn't work as it appends .SSSZ at the end. Gives me the following output The specified value "2017-04-10T17:02:00.320Z" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS"..

Does anyone have any clean solution to this problem?

like image 458
spalonytoster Avatar asked Apr 10 '17 17:04

spalonytoster


People also ask

How does one convert Date object to string in JavaScript?

JavaScript Date toString() The toString() method returns a date object as a string.

How to extract date and time from date object in JavaScript?

If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

How to format datetime local?

DD/MM/YYYY. MM/DD/YYYY.

What is the difference between datetime and datetime local?

The difference between the two is that the datetime-local input does not include the time zone. If the time zone is not important to your application, use datetime-local. Some browsers are still trying to catch up to the datetime input type.


1 Answers

I used moment.js library to format the date accordingly. This line of code does the trick moment(date).format("YYYY-MM-DDTkk:mm").

like image 122
spalonytoster Avatar answered Sep 20 '22 13:09

spalonytoster