How can I convert this string to a javascript Date object?

Weblurk

I have a string with a specific format which I am trying to convert to a Date object. I was hoping var dateObject = new Date(myString) would be sufficient but it won't work. So I tried breaking up the string into smaller pieces, and that didn't work either.

var str = '2015-02-20 11:00'; 
var obj = new Date(str.substr(2, 4), str.substr(5, 7), str.substr(8, 10), str.substr(13, 15), str.substr(16, 18));
// Above code yields invalid date

JS-Fiddle Example

How can I make a date object out of my string?

adeneo

Did you try just

var str = '2015-02-20 11:00';
var obj = new Date(str);

works for me -> http://jsfiddle.net/j5kaC/1/

If for some strange reason that doesn't work, split it up and pass it to the Date constructor in the appropriate places

var str = '2015-02-20 11:00';

var arr  = str.split(' ');
var time = arr.pop().split(':')
var arr2 = arr.shift().split('-');

var date = new Date(arr2[0], arr2[1]-1, arr2[2], time.shift(), time.pop(), 0, 0);

console.log(date);

FIDDLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I parse a date (provided as a string) in the "ddMM" format and convert it to a Date Object in Java?

how can i convert object into an array in javascript?

How to convert dd/mm/yyyy string into JavaScript Date object?

How can I convert a string to boolean in JavaScript?

How can I convert year and week to Java Date object?

How can I convert string date to NSDate?

Android, How can I Convert String to Date?

Javascript How to convert a non-UTC Date string into the Date object?

How can I convert a string into a date?

How can I convert a string to object in Javascript?

How can I convert date string to data in javascript in different timezone?

How can I convert "/Date(1399739515000)/" into date format in JavaScript?

How can i convert a string object to string pool object?

JS : How can i convert the string ("Mar 10, 2015") to a Date object?

How I can convert array to object in Javascript

How can I convert sperated date string to consecutive date string

How can I convert string date to NSDate?

How to convert a time string to Date object on Javascript?

Javascript convert date string to object

How can I convert string to array in JavaScript?

How can I convert a nested javascript object into an inline string of CSS styles?

How can i convert an object to the structure { [param: string]: string | string[] }?

How can I convert my date object to use it in Symfony formbuilder?

How can I convert Date JsonObject to String?

How can I convert date to string in python?

how to convert date object to string using javascript

How to convert a Javascript Date String into a Javascript Date Object?

How can I convert a specific string date to date or datetime in Spark?

How can I convert a single object of a json string into a .Net object?