Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to Date Javascript

Trying to convert a string to Javascript Date. My string:

var fullDate = this.date + " " + this.time + ":00";

gives an output: 24/12/2016 02:00:00

but trying to convert it in js Date object like:

 var k = new Date(fullDate); 

gives the output: Invalid Date

or even: var k = Date.parse(fullDate);

gives the output: NaN

Any help is welcome.

I was following instructions from http://www.datejs.com/ so far

like image 743
Georgios Avatar asked Dec 13 '16 14:12

Georgios


People also ask

What is toISOString in JavaScript?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy:Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

What is Date now () in JavaScript?

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.


1 Answers

Your format is invalid. Valid format is month/day/year, so try:

var fullDate = "12/24/2016 02:00:00";
var k = new Date(fullDate);

Hope I could help!

like image 67
Sebastian Kaczmarek Avatar answered Sep 29 '22 04:09

Sebastian Kaczmarek